-
Notifications
You must be signed in to change notification settings - Fork 2
/
targetdetection.py
executable file
·57 lines (48 loc) · 1.98 KB
/
targetdetection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import cv2
import numpy as np
import serial
import face_recognition
face_cascade = cv2.CascadeClassifier("F://haarcascade_frontalface_default.xml")
focalLength=3.85
baseline=60
ser=serial.Serial("com5",9600)
#capturing the video images
cap = cv2.VideoCapture(1)
cap2 = cv2.VideoCapture(2)
while True:
ret, img = cap.read()
ret,img2 = cap2.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#converting the image to gray_image
gray2=cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5) #using haarcascade classifier to detect the frontal face
for (x,y,w,h) in faces: #making a rectangle around the frontal face
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
#finding the centroid of the detected face
cX = x + int(w/2)
cY = y + int(h/2)
cv2.circle(img, (cX, cY), 5, (255, 255, 255), -1)
cv2.putText(img, "centroid", (cX - 25, cY - 25),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
#calculating the disparity map
stereo = cv2.StereoBM_create(numDisparities=128, blockSize=7)
disparity = stereo.compute(gray,gray2)
min = disparity.min()
max = disparity.max()
#Calculating the depth map using disparity
#converting disparity into mm
depth = (focalLength * baseline) / (disparity*0.264)
print(cX)
print(cY)
print(-disparity)
cX=(int)(cX/480) #Scaling the centroid co-ordinates
cY=(int)(cY/480)
print(-depth[cX,cY])
ser.write(cX)
ser.write(cY)
ser.write(-depth[cX,cY])
cv2.imshow('img' , img)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()