繁体   English   中英

根据检测范围拍照-Python + OpenCV

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM