-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.py
61 lines (46 loc) · 1.72 KB
/
detect.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
import cv2
import cv2 as cv
import numpy as np
import urllib.request
x = int(input('Where do you get the input? \n 0 = local \n 1 = url \n 2 = cam \n Answer(Number) = '))
face_model = cv.CascadeClassifier('facedetectionmodel.xml')
def detection():
gray_scale = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
faces = face_model.detectMultiScale(gray_scale)
for (x,y,w,h) in faces:
cv.rectangle(img, (x,y), (x+w,y+h), (255,255,0), 2) #!RGB --> BGR
cv.imshow('image', img)
cv.waitKey(0)
cv.destroyAllWindows()
def detection2(img):
gray_scale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_model.detectMultiScale(gray_scale, 1.3, 5)
if faces is ():
return img
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (255,255,0), 2) #!RGB --> BGR
return img
if x == 0:
img = cv.imread('tag.ong')
detection()
elif x == 1:
img_url = str(input('url = '))
#img_url = input('url photo = ')
# ดาวน์โหลดภาพจาก URL
with urllib.request.urlopen(img_url) as url_response:
img_array = np.array(bytearray(url_response.read()), dtype=np.uint8)
# แปลงข้อมูลภาพเป็น array
img = cv.imdecode(img_array, -1)
detection()
elif x == 2:
cam_select = int(input('Which camera? \n (if you have only 1 cam. enter 0, Default = 0) \n Answer = '))
img = cv2.VideoCapture(cam_select)
#cam = cv2.VideoCapture(0)
while True:
ret, frame = img.read()
frame = detection2(frame)
cv2.imshow('Face Detection Cam', frame)
if cv2.waitKey(1) & 0xFF == ord('q'): #? กด Q เพื่อออก
break
img.release()
cv2.destroyAllWindows()