下面提到的代码是我想要的实用程序我希望我的代码识别我的脸并点亮led而它是真的。我明白我需要在框出现时输入true它基本上会提供0 /我的ardiuno有1个布尔逻辑,但我不知道如何实现它。
我的尝试
import cv2
import sys
import serial
cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascPath)
ser = serial.Serial('/dev/ttyACM0',9600)
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags= cv2.CASCADE_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
我想要的功能
import serial
import cv
loop = True
cap = cv.CreateCameraCapture(0)
ser = serial.Serial('/dev/ttyACM1', 9600)
while loop:
# Read image
temp = cv.QueryFrame(cap)
# Create another image to hold Black&White version of image
image = cv.CreateImage((temp.width, temp.height), cv.IPL_DEPTH_8U, 1)
# Convert original image to B/W - Only because calculations are apparently better
cv.CvtColor(temp, image, cv.CV_BGR2GRAY)
# Use Haar Classifier for face detection (Magic Black Box)
storage = cv.CreateMemStorage()
haar=cv.Load('./haarcascade_frontalface_default.xml')
detected = cv.HaarDetectObjects(image, haar, storage, 1.2, 2,cv.CV_HAAR_DO_CANNY_PRUNING, (100,100))
# Returns a list, the first item of which is a tuple
# First element of tuple is x, second is y, third and fourth are width and height
# We pass these params to create a rectangle. If we had not converted a b/w image
# we can have a red border around instead
if detected:
cv.Rectangle(image, (detected[0][0][0], detected[0][0][1]), (detected[0][0][0] + detected[0][0][2], detected[0][0][1] + detected[0][0][3]), cv.RGB(255, 0, 0), 3)
ser.write('Just a really really long string to see something')
# Create a named window and display image in it. Key press kills window and exits
cv.NamedWindow('Nik')
cv.ShowImage('Nik', image)
char = cv.WaitKey(33)
if (char != -1):
if (ord(char) == 27):
loop = False
我设法解决了它并在github上传了代码,任何人都感兴趣。 Face-Detection+Ardiuno