This is my code below, It will output that face gets recognize but now I want to print out the coordinates of x and y that the person is getting recognized. Would I include the print statement for x and y coordinates in the for loop or outside of it? Please help! Thanks!
import numpy as np
import cv2
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
prevTime = 0
## This will get our web camera
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
while True:
retval, frame = cap.read()
if not retval:
break
_, img = cap.read() ## This gets each frame from the video, cap.read returns 2 variables flag - indicate frame is correct and 2nd is f
##img = cv2.imread('Z.png') Then we get our image we want to use
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # This method only works on gray skin images, so we have to convert the gray scale to rgb image
faces = face_cascade.detectMultiScale(gray, 1.1, 5) ## Next, we detect the faces
if len(faces) > 0:
print("[INFO] found {0} faces!".format(len(faces)))
GPIO.output(18,GPIO.HIGH)
else:
print("No face")
GPIO.output(18,GPIO.LOW)
for (x, y, w, h) in faces: ## We draw a rectangle around the faces so we can see it correctly
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0)) ## The faces will be a list of coordinates
cv2.putText(img, 'Myface', (x, y), font, fontScale=1, color=(255,70,120),thickness=2)
cv2.putText(frame, 'Number of Faces Detected: ' + str, (0, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0))
cv2.imshow('img', img) ## Last we show the image
x = cv2.waitKey(30) & 0xff
if x==27:
break
## Press escape to exit the program
cap.release()
Put it inside the loop. Try this:
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
for (x, y, w, h) in faces:
x1 = x
x2 = x + w
y1 = y
y2 = y + h
print ("diaginal point 1 (x1,y1) = ({},{})".format(x1, y1))
print ("diaginal point 2 (x2,y2) = ({},{})".format(x2, y2))
But if you don't want it to print it all the time and print it once if you press specific key like "P" or "p", then use this code:
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
for (x, y, w, h) in faces:
x1 = x
x2 = x + w
y1 = y
y2 = y + h
k = cv2.waitKey(10) x00F
if k == ord("p"):
print ("diaginal point 1 (x1,y1) = ({},{})".format(x1, y1))
print ("diaginal point 2 (x2,y2) = ({},{})".format(x2, y2))
So whenever you press "p," it will print the x and y coordinate of point1 and 2:) Just for simplicity, I mention x1, y1, x2, and y2.
I hope it helps.