Hey @Adrian284751,
Assuming you are using the code provided in this guide it should be fairly easy to invert the pan and tilt functions.
The following code is a slightly modified version of the code provided in the guide with these values inverted. Let me know if this works for you!
#!/usr/bin/env python
import cv2, sys, time, os
from pantilthat import *
os.system('sudo modprobe bcm2835-v4l2')
os.system('v4l2-ctl -p 40')
FRAME_W = 320
FRAME_H = 200
cam_pan = 40
cam_tilt = 20
cascPath = '/usr/share/opencv/lbpcascades/lbpcascade_frontalface.xml'
faceCascade = cv2.CascadeClassifier(cascPath)
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 200)
time.sleep(2)
pan(cam_pan-90)
tilt(cam_tilt-90)
light_mode(WS2812)
def lights(r,g,b,w):
for x in range(18):
set_pixel_rgbw(x,r if x in [3,4] else 0,g if x in [3,4] else 0,b,w if x in [0,1,6,7] else 0)
show()
lights(0,0,0,50)
while True:
ret, frame = cap.read()
frame = cv2.flip(frame, -1)
if ret == False:
print("Error getting image")
continue
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
faces = faceCascade.detectMultiScale(frame, 1.1, 3, 0, (10, 10))
lights(50 if len(faces) == 0 else 0, 50 if len(faces) > 0 else 0,0,50)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 4)
#Get the centre of the face
#Inverted operations by adding instead of subtracting
x = x + (w/2)
y = y + (h/2)
#Correct relative to centre of image
#Inverted operations by adding instead of subtracting
#These changes result in the camera moving in the opposite direction
turn_x = float(x + (FRAME_W/2))
turn_y = float(y + (FRAME_H/2))
#Convert to percentage offset
turn_x /= float(FRAME_W/2)
turn_y /= float(FRAME_H/2)
#Scale offset to degrees
#Inverted operation by negating to result in inverted offset
turn_x *= -2.5 # VFOV
turn_y *= -2.5 # HFOV
cam_pan = -turn_x
cam_tilt = turn_y
cam_pan = max(0,min(180,cam_pan))
cam_tilt = max(0,min(180,cam_tilt))
# Update the servos
pan(int(cam_pan-90))
tilt(int(cam_tilt-90))
break
frame = cv2.resize(frame, (540,300))
frame = cv2.flip(frame, 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
cap.release()
cv2.destroyAllWindows()
Hope this helps!