-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detection.py
54 lines (43 loc) · 2 KB
/
face_detection.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
import cv2
import numpy as np
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
modelFile = "res10_300x300_ssd_iter_140000_fp16.caffemodel"
configFile = "deploy.prototxt"
net = cv2.dnn.readNetFromCaffe(configFile, modelFile)
class App:
def __init__(self, window, window_title, video_source=0):
self.window = window
self.window.title(window_title)
self.video_source = video_source
self.vid = cv2.VideoCapture(video_source)
if not self.vid.isOpened():
raise ValueError("Unable to open video source", video_source)
self.canvas = Canvas(window, width=self.vid.get(cv2.CAP_PROP_FRAME_WIDTH), height=self.vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.canvas.pack()
self.delay = 20
self.update()
self.window.mainloop()
def detect_face(self, frame):
blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), (104.0, 177.0, 123.0))
net.setInput(blob)
detections = net.forward()
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > 0.7: # Filtrar por confiança para reduzir falsos positivos
box = detections[0, 0, i, 3:7] * np.array([frame.shape[1], frame.shape[0], frame.shape[1], frame.shape[0]])
(x, y, x1, y1) = box.astype("int")
cv2.rectangle(frame, (x, y), (x1, y1), (0, 255, 0), 2)
forehead_x = x + (x1 - x) // 2
forehead_y = y + (y1 - y) // 5
cv2.circle(frame, (forehead_x, forehead_y), 5, (0, 0, 255), -1)
return frame
def update(self):
ret, frame = self.vid.read()
if ret:
frame = self.detect_face(frame)
self.photo = ImageTk.PhotoImage(image=Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)))
self.canvas.create_image(0, 0, image=self.photo, anchor=NW)
self.window.after(self.delay, self.update)
App(tk.Tk(), "Face Detection and Forehead Pointing")