
Mastering Manual Interaction for Dynamic Surveillance
Step into the driver’s seat of your dynamic vision setup as we explore the thrilling realm of manual control. In this guide, we’ll dive into the process of harnessing the power of keyboard inputs to navigate and control pan/tilt servos and a camera in real-time using OpenCV with Python. From understanding the key mapping to implementing responsive servo movements, this tutorial puts you in command, opening up a world of possibilities for interactive surveillance and dynamic vision applications. Let’s embark on the journey of hands-on control!
What’s Covered
- Key Mapping.
- The Control Program.
Key Mapping
We can easily find a keys corresponding key code with a simple Python script:
import cv2 print('Starting Key Test') camera=cv2.VideoCapture(0) while(1): ret,frame=camera.read() cv2.imshow('Camera',frame) k = cv2.waitKey(33) if k==27: # Esc key to stop break elif k==-1: # normally -1 returned,so don't print it continue else: print (k) # else print its value
In the above example, when the code is run, the camera stream will open. While the camera stream is open, if any key on your keyboard is pressed, the corresponding key value will be printed to the terminal console.
Manually Control Pan/Tilt Servos Program
Hopefully, your key codes are the same as in the code below, but just in case they are not you can use the key mapping code above to find their code values.
Open Visual Studio then copy and paste the code below. In the Python code below, you now have complete manual control for the field-of-view the camera has by simple using the cursor keys on a keyboard.
Python Code:
import cv2 import os from adafruit_servokit import ServoKit #Servo settings pan=100 tilt=150 kit=ServoKit(channels=16) kit.servo[0].angle=pan kit.servo[1].angle=tilt #Camera settings height=480 width=640 camera = cv2.VideoCapture(0) camera.set(cv2.CAP_PROP_FRAME_WIDTH,width) camera.set(cv2.CAP_PROP_FRAME_HEIGHT,height) while True: ret, frame=camera.read() cv2.imshow('PanTilt',frame) kit.servo[0].angle=pan kit.servo[1].angle=tilt #Use OpenCV waitkey for user control k = cv2.waitKey(33) #Terminate program if k==27: #27 = ESC key break #Servo control if k==81: #81 = left cursor key pan+=5 print(pan) if k==83: #83 = right cursor key pan=pan-5 print(pan) if k==84: #84 = down cursor key tilt+=5 print(tilt) if k==82: #82 = up cursor key tilt=tilt-5 print(tilt) #Set range limits to stop out of range errors if pan>170: pan=170 print('pan out of range') if pan<0: pan=0 print('pan out of range') if tilt>180: tilt=180 print('tilt out of range') if tilt<0: tilt=0 print('tilt out of range') camera.release() cv2.destroyAllWindows() #Reset servos kit.servo[0].angle=100 kit.servo[1].angle=150 print('Program Terminated')
Conclusion
Congratulations, commander of vision! You’ve successfully unlocked the potential of hands-on control, steering your pan/tilt servos and camera with the precision of keyboard inputs using OpenCV with Python. By mastering this manual interaction, your projects are now poised for dynamic surveillance and real-time responsiveness.
As we conclude this journey of hands-on control, stay tuned for our finale post in this OpenCV for Beginners guide where we’ll explore our last avenue of dynamic vision. Happy navigating, and may your projects thrive with interactive ingenuity!
In the last installment for our OpenCV for Beginners guide we will be Creating a Zoom Function for our camera projects.
That’s All Folks!
You can find all of our OpenCV guides here: OpenCV for Beginners