-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp_webcam_cv3.py
96 lines (79 loc) · 2.1 KB
/
temp_webcam_cv3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python
import cv2
import sys
import logging as log
import datetime as dt
from time import sleep
import os #ANNA's changes
###
import time
from PIL import Image
import numpy as np
import pickle
import matplotlib.pyplot as plt
###
Tstart = time.time()
index= 0
###
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
log.basicConfig(filename='webcam.log',level=log.INFO)
video_capture = cv2.VideoCapture(1)
anterior = 0
while True:
if not video_capture.isOpened():
print('Unable to load camera.')
sleep(5)
pass
# 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)
)
# Display the resulting frame
cv2.imshow('Video', frame)
if faces == ():
continue
x1=0
y1=0
x2=0
y2=0
# 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)
x1=x
y1=y
x2=x+w
y2=y+h
if anterior != len(faces):
anterior = len(faces)
log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.now()))
###
# Ttemp = time.time()
# if Ttemp - Tstart > 2:
# cv2.imwrite(str(index)+"s.jpg",frame)
# index = index + 1
# Tstart = Ttemp
###
Ttemp = time.time()
if Ttemp - Tstart > 0.8:
if not os.path.exists("oripics"):
os.makedirs("oripics")
cv2.imwrite("oripics\\"+str(index)+"s.jpg",frame)
box = (x1*0.97,y1*0.97,x2*1.03,y2*1.03)
im = Image.open("oripics\\"+str(index)+"s.jpg")
region = im.crop(box)
if not os.path.exists("editpics"):
os.makedirs("editpics")
region.save("editpics\\"+str(index)+"c.png","PNG")
index += 1
Tstart = Ttemp
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()