-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.py
More file actions
76 lines (60 loc) · 2.19 KB
/
Copy pathtempCodeRunnerFile.py
File metadata and controls
76 lines (60 loc) · 2.19 KB
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
import cv2
import numpy as np
import pyautogui
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
# Initialize the webcam
cap = cv2.VideoCapture(0)
# Define the regions of interest (ROIs) for different actions
roi_gas = (100, 300, 150, 450) # (top, bottom, left, right)
roi_brake = (300, 500, 150, 450)
# Initialize background subtractor
fgbg = cv2.createBackgroundSubtractorMOG2()
def detect_motion(roi, frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
fgmask = fgbg.apply(gray)
roi_frame = fgmask[roi[0]:roi[1], roi[2]:roi[3]]
contours, _ = cv2.findContours(roi_frame, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) > 0:
largest_contour = max(contours, key=cv2.contourArea)
if cv2.contourArea(largest_contour) > 500: # Adjust the threshold as needed
return True
return False
def update_plot(frame):
plt.clf()
plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
plt.draw()
# Create the main window
root = tk.Tk()
root.title("Motion Detection - Hill Climb Racing")
# Create a figure for matplotlib
fig, ax = plt.subplots()
# Create a canvas for matplotlib
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
while True:
ret, frame = cap.read()
if not ret:
break
# Detect motion in each ROI
if detect_motion(roi_gas, frame):
pyautogui.press('right') # Simulate gas
if detect_motion(roi_brake, frame):
pyautogui.press('left') # Simulate brake
# Draw the ROIs on the frame
cv2.rectangle(frame, (roi_gas[2], roi_gas[0]), (roi_gas[3], roi_gas[1]), (0, 255, 0), 2)
cv2.rectangle(frame, (roi_brake[2], roi_brake[0]), (roi_brake[3], roi_brake[1]), (255, 0, 0), 2)
# Update the plot with the current frame
update_plot(frame)
canvas.draw()
# Display the frame
cv2.imshow('Motion Detection - Hill Climb Racing', frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcam and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()
# Start the Tkinter main loop
root.mainloop()