程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python practice_ Save face recognition pictures in video files

編輯:Python

Use existing video files , Practice the lower part and eye judgment , Save the qualified areas , There are many misjudgments .

import cv2

# Face recognition and eye recognition loading
faceCascade = cv2.CascadeClassifier(“./venv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml”)
faceCascade_eye = cv2.CascadeClassifier(“./venv/Lib/site-packages/cv2/data/haarcascade_eye.xml”)

# Read video file
video = cv2.VideoCapture(‘./image/dl10.mp4’)

# Video rate
V_FPS = video.get(cv2.CAP_PROP_FPS)
V_COUNT = video.get(cv2.CAP_PROP_FRAME_COUNT)
# Video width height
V_WIDTH = video.get(cv2.CAP_PROP_FRAME_WIDTH)
V_HEIGHT = video.get(cv2.CAP_PROP_FRAME_HEIGHT)

print(‘V_FPS:’,V_FPS,‘V_COUNT:’,V_COUNT,‘V_WIDTH’,V_WIDTH,‘V_HEIGHT’,V_HEIGHT,‘ Duration seconds :’,str(round(V_COUNT/V_FPS)))

num = 0 # Defining variables , Used for file name tags
while (video.isOpened()):
retval,image = video.read()
MSEC = video.get(cv2.CAP_PROP_POS_MSEC)
if round(MSEC/1000,2)%10 ==1:
print(‘ Duration seconds :’,str(round(V_COUNT/V_FPS)),‘MSEC’,str(round(MSEC/1000,2)))

#cv2.namedWindow('video',0)
#cv2.resizeWindow('video',1280,720)
if retval == True:
#cv2.imshow('video',image)
faces = faceCascade.detectMultiScale(image, 1.15)
#print('faces Number :',len(faces),faces)
faces_num = 0
for (x, y, w, h) in faces:
#print('xywh', (x, y, w, h))
if w >= 50: # Image width exceeds 50 Just save , Because too little misjudgment is more likely
cropped = image[y:y + w, x:x + h] # The cutting coordinate is [y0:y1, x0:x1]
# Add eye recognition , If there are eyes in storage
eye = faceCascade_eye.detectMultiScale(cropped, 1.15)
if len(eye) > 0:
print(' eyes :',len(eye))
cv2.imwrite("./image/weimi_meinv/meinv_" + str(num)+'_'+str(faces_num) + '.jpeg', cropped)
faces_num += 1
num += 1
else:
break
key = cv2.waitKey(1)
if key == 27:
break

video.release()
cv2.destroyAllWindows()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved