[英]Take a picture based on the scale of detection- Python + OpenCV
以下是我的通過網絡攝像頭檢測面部的代碼。
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('C:\OpenCV2.0\data\haarcascades\haarcascade_frontalface_default.xml')
img = cv2.VideoCapture(0)
while(1):
_,f=img.read()
gray = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
detect_frame = cv2.rectangle(f,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('img',f)
if cv2.waitKey(25) == 27:
break
cv2.destroyAllWindows()
img.release()
通過此代碼,我想在實現變幀或人在移動時拍照。 拍照后,它將照片保存在文件中,然后繼續其工作。
你們可以幫我解決這個問題的方法嗎? 非常感謝您的熱情。
這是我編寫的代碼,它的作用是首先找到通過面部檢測獲得的矩形的中心。 然后,它將在一定的時間間隔(如2s)之后比較中心坐標的值,如果像素的值已更改為超過某個閾值,則表明檢測到了運動。 該代碼工作正常。 為了提高精度,您可以在條件中添加更多點,其值的變化將指示運動。
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
i=1000
c=10
while 1:
_, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
#we will increment the value of i after every loop so that we could check value of coordinate after a certain time
i=i+1
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,150,10),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
#finding the center of the rectangle(around face)
a=y+h/2
b=x+w/2
d = x + w / 2
if i % 5 == 0:
print (abs(a - c))
if (abs(a-c))>9: #change this value to calibrate
print("Movement Detected")
c = y + h / 2
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
如果i%5 == 0,我們需要在一段時間后更新值以進行比較,其中每個循環后i都會遞增
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.