rj8005 commited on
Commit
dca4ca2
·
verified ·
1 Parent(s): 68ba5d2

Upload 3 files

Browse files
facedetection.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import time
4
+
5
+ faceClassifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
6
+
7
+ videoCam = cv2.VideoCapture(0)
8
+
9
+ if not videoCam.isOpened():
10
+ print("Camera cannot be accessed")
11
+ exit()
12
+
13
+ qPressed = False
14
+ while (qPressed == False):
15
+ ret, frame = videoCam.read()
16
+
17
+ if ret == True:
18
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
19
+ faces = faceClassifier.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=2)
20
+
21
+ for (x, y, w, h) in faces:
22
+ cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
23
+
24
+ # print("Number of detected faces: ", len(faces))
25
+ text = "Number of Detected Faces = " + str(len(faces))
26
+
27
+ font = cv2.FONT_HERSHEY_SIMPLEX
28
+ cv2.putText(frame, text, (0, 30), font, 1, (255, 0, 0), 1)
29
+
30
+ cv2.imshow("Result", frame)
31
+ if cv2.waitKey(1) & 0xFF == ord('q'):
32
+ qPressed = True
33
+ break
34
+
35
+ videoCam.release()
36
+ cv2.destroyAllWindows()
facetracking.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ from cvzone.FaceDetectionModule import FaceDetector
3
+ import numpy as np
4
+ import time
5
+
6
+ cap = cv2.VideoCapture(0)
7
+ ws, hs = 1280, 720
8
+ cap.set(3, ws)
9
+ cap.set(4, hs)
10
+
11
+ if not cap.isOpened():
12
+ print("Camera couldn't Access!!!")
13
+ exit()
14
+
15
+ detector = FaceDetector()
16
+ prev_face_x, prev_face_y = None, None
17
+ prev_time = time.time()
18
+
19
+ while True:
20
+ success, img = cap.read()
21
+ img, bboxs = detector.findFaces(img, draw=False)
22
+
23
+ if bboxs:
24
+ # Get the head position (top-center of the bounding box)
25
+ head_x = bboxs[0]["bbox"][0] + bboxs[0]["bbox"][2] // 2
26
+ head_y = bboxs[0]["bbox"][1]
27
+
28
+ # Draw the red circle at the head position
29
+ cv2.circle(img, (head_x, head_y), 80, (0, 0, 255), 2)
30
+ cv2.circle(img, (head_x, head_y), 15, (0, 0, 255), cv2.FILLED)
31
+ cv2.putText(img, "TARGET LOCKED", (850, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
32
+
33
+ # Calculate face motion trajectory and speed
34
+ if prev_face_x is not None and prev_face_y is not None:
35
+ # Calculate motion trajectory
36
+ trajectory = np.sqrt((head_x - prev_face_x) ** 2 + (head_y - prev_face_y) ** 2)
37
+
38
+ # Calculate time difference
39
+ current_time = time.time()
40
+ time_diff = current_time - prev_time
41
+
42
+ # Calculate speed
43
+ speed = trajectory / time_diff
44
+
45
+ # Print motion trajectory and speed
46
+ print("Motion Trajectory:", trajectory)
47
+ print("Speed:", speed, "pixels per second")
48
+
49
+ # Draw motion trajectory line
50
+ cv2.line(img, (prev_face_x, prev_face_y), (head_x, head_y), (0, 255, 0), 2)
51
+
52
+ # Draw arrow to indicate direction of motion
53
+ cv2.arrowedLine(img, (prev_face_x, prev_face_y), (head_x, head_y), (255, 0, 0), 2)
54
+
55
+ # Display speed value
56
+ cv2.putText(img, f"Speed: {speed:.2f} pixels per second", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1,
57
+ (255, 255, 255), 2)
58
+
59
+ # Update previous face position and time
60
+ prev_face_x, prev_face_y = head_x, head_y
61
+ prev_time = current_time
62
+ else:
63
+ # Initialize previous face position and time
64
+ prev_face_x, prev_face_y = head_x, head_y
65
+ prev_time = time.time()
66
+ else:
67
+ cv2.putText(img, "NO TARGET", (880, 50), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255), 3)
68
+ cv2.circle(img, (640, 360), 80, (0, 0, 255), 2)
69
+ cv2.circle(img, (640, 360), 15, (0, 0, 255), cv2.FILLED)
70
+
71
+ cv2.imshow("Image", img)
72
+ if cv2.waitKey(1) & 0xFF == ord('q'):
73
+ break
74
+
75
+ cap.release()
76
+ cv2.destroyAllWindows()
haarcascade_frontalface_default.xml ADDED
The diff for this file is too large to render. See raw diff