-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpendulum2.py
68 lines (52 loc) · 1.96 KB
/
pendulum2.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
import cv2 as cv
import numpy as np
from threading import Thread
from pygame import mixer
# Constants
VIDEO_WIDTH = 640
VIDEO_HEIGHT = 480
RIGHT_CENTER_X_THRESHOLD = (VIDEO_WIDTH / 2) + 50 # Adjust this value based on your video frame width
LEFT_CENTER_X_ThRESHOLD = (VIDEO_WIDTH / 2) - 50
DIST_THRESHOLD = 60 # Threshold for distance comparison
videoCapture = cv.VideoCapture(0)
# Reduce the resolution
videoCapture.set(cv.CAP_PROP_FRAME_WIDTH, VIDEO_WIDTH)
videoCapture.set(cv.CAP_PROP_FRAME_HEIGHT, VIDEO_HEIGHT)
prevCircle = None
mixer.init(channels=64)
sound = mixer.Sound('Piano_C3.wav')
channel_number = 0
def play_sound():
global channel_number
mixer.Channel(channel_number).play(sound)
channel_number = (channel_number + 1) % mixer.get_num_channels()
while True:
ret, frame = videoCapture.read()
if not ret:
break
grayFrame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
blurFrame = cv.GaussianBlur(grayFrame, (17, 17), 0)
circles = cv.HoughCircles(blurFrame, cv.HOUGH_GRADIENT, 1.2, minDist=100, param1=50, param2=30, minRadius=15, maxRadius=55)
if circles is not None:
circles = np.uint16(np.around(circles))
chosen = None
for i in circles[0, :]:
if chosen is None:
chosen = i
else:
dist = np.linalg.norm(chosen[:2] - i[:2])
if dist <= DIST_THRESHOLD:
chosen = i
center_x = chosen[0]
radius = chosen[2]
# Draw circle and center point on the frame
cv.circle(frame, (chosen[0], chosen[1]), radius, (0, 100, 100), 3)
cv.circle(frame, (chosen[0], chosen[1]), 1, (0, 100, 100), 3)
# Check if the object crosses the center x-axis
if center_x < RIGHT_CENTER_X_THRESHOLD and center_x > LEFT_CENTER_X_ThRESHOLD:
play_sound()
cv.imshow("circles", frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
videoCapture.release()
cv.destroyAllWindows()