-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithubcv2
96 lines (78 loc) · 2.81 KB
/
githubcv2
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import subprocess
import time
import cv2
import numpy as np
import RPi.GPIO as GPIO
from picamera.array import PiRGBArray
from picamera import PiCamera
# Install OpenCV and dependencies
subprocess.call("sudo apt-get update", shell=True)
subprocess.call("sudo apt-get install -y libatlas-base-dev libhdf5-dev libhdf5-serial-dev libqtgui4 libqt4-test", shell=True)
subprocess.call("pip install opencv-python-headless", shell=True)
# Stepper motor configuration
step_pin1 = 17
dir_pin1 = 27
step_pin2 = 23
dir_pin2 = 24
# OpenCV parameters
scale_factor = 0.5
# Initialize GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(step_pin1, GPIO.OUT)
GPIO.setup(dir_pin1, GPIO.OUT)
GPIO.setup(step_pin2, GPIO.OUT)
GPIO.setup(dir_pin2, GPIO.OUT)
# Set initial direction
GPIO.output(dir_pin1, GPIO.LOW) # Set direction to clockwise
GPIO.output(dir_pin2, GPIO.LOW) # Set direction to clockwise
# Initialize PiCamera
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 30
raw_capture = PiRGBArray(camera, size=(640, 480))
# Allow the camera to warm up
time.sleep(2)
# Loop to track motion
for frame in camera.capture_continuous(raw_capture, format="bgr", use_video_port=True):
# Capture frame-by-frame
image = frame.array
# Resize frame
image = cv2.resize(image, None, fx=scale_factor, fy=scale_factor)
# Convert frame to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Perform motion detection
# Assuming you have the motion detection logic implemented and the coordinates of motion as 'x' and 'y'
# Calculate center point
frame_height, frame_width = gray.shape
center_x = frame_width // 2
center_y = frame_height // 2
# Calculate error
error_x = center_x - x
error_y = center_y - y
# Move stepper motors based on the error
# Adjust the speed and step_delay according to your stepper motor's specifications
step_delay = 0.001
steps_per_rev = 200
speed = 10
if error_x > 0:
GPIO.output(dir_pin1, GPIO.HIGH) # Set direction to counterclockwise
for _ in range(speed):
GPIO.output(step_pin1, GPIO.HIGH)
time.sleep(step_delay)
GPIO.output(step_pin1, GPIO.LOW)
time.sleep(step_delay)
if error_x < 0:
GPIO.output(dir_pin1, GPIO.LOW) # Set direction to clockwise
for _ in range(speed):
GPIO.output(step_pin1, GPIO.HIGH)
time.sleep(step_delay)
GPIO.output(step_pin1, GPIO.LOW)
time.sleep(step_delay)
if error_y > 0:
GPIO.output(dir_pin2, GPIO.HIGH) # Set direction to counterclockwise
for _ in range(speed):
GPIO.output(step_pin2, GPIO.HIGH)
time.sleep(step_delay)
GPIO.output(step_pin2, GPIO.LOW)
time.sleep(step_delay)
if error_y < 0