-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmevlut
161 lines (121 loc) · 3.95 KB
/
mevlut
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from flask import Flask, render_template
#!usr/bin/python
import numpy as np
import cv2
from time import sleep
import RPi.GPIO as GPIO
import google.assistant.library as google
Forward = 18
Backward = 23
Forward2 = 22
Backward2 = 27
from flask import (
Flask,
render_template,
request,
)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(Forward, GPIO.OUT)
GPIO.setup(Backward, GPIO.OUT)
GPIO.setup(Forward2, GPIO.OUT)
GPIO.setup(Backward2, GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(20,GPIO.OUT)
pwm = GPIO.PWM(16, 100)
pwm.start(10)
pwm2 = GPIO.PWM(20, 100)
pwm2.start(10)
GPIO.output(Forward, GPIO.LOW)
GPIO.output(Forward2, GPIO.LOW)
app = Flask(__name__)
@app.route('/setMotor', methods=['GET', 'POST'])
#/setMotor?motor=forward
def setMotor():
pin = Forward
pin2 = Forward2
motor = request.values['motor']
if motor.lower().__contains__("forward"):
pin = Forward
pin2 = Forward2
elif motor.lower().__contains__("backward"):
pin = Backward
pin2 = Backward2
GPIO.output(pin, GPIO.HIGH)
GPIO.output(pin2, GPIO.HIGH)
sleep(5)
GPIO.output(pin, GPIO.LOW)
GPIO.output(pin2, GPIO.LOW)
return "Motor: %s" % (motor)
@app.route('/turnMotor', methods=['GET', 'POST'])
#/turnMotor?motor=right
def turnMotor():
pin = 0
pin2 = 0
motor = request.values['motor']
if motor.lower().__contains__("right"):
pin = Forward2
pin2 = Backward
elif motor.lower().__contains__("left"):
pin = Backward2
pin2 = Forward
GPIO.output(pin, GPIO.HIGH)
GPIO.output(pin2, GPIO.HIGH)
sleep(5)
GPIO.output(pin, GPIO.LOW)
GPIO.output(pin2, GPIO.LOW)
return "Motor: %s" % (motor)
@app.route('/followLine', methods=['GET', 'POST'])
#/followLine
def followline():
video = cv2.VideoCapture(-1)
video.set(3, 160)
video.set(4, 120)
GPIO.output(Forward, GPIO.HIGH)
GPIO.output(Forward2, GPIO.HIGH)
while(True):
# Capture the frames
ret, frame = video.read()
# Crop the image
crop_img = frame[60:120, 0:160]
# Convert to grayscale
gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
# Gaussian blur
blur = cv2.GaussianBlur(gray,(5,5),0)
# Color thresholding
ret,thresh1 = cv2.threshold(blur,60,255,cv2.THRESH_BINARY_INV)
# Erode and dilate to remove accidental line detections
mask = cv2.erode(thresh1, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# Find the contours of the frame
contours,hierarchy = cv2.findContours(mask.copy(), 1, cv2.CHAIN_APPROX_NONE)
# Find the biggest contour (if detected)
if len(contours) > 0:
c = max(contours, key=cv2.contourArea)
M = cv2.moments(c)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
cv2.line(crop_img,(cx,0),(cx,720),(255,0,0),1)
cv2.line(crop_img,(0,cy),(1280,cy),(255,0,0),1)
cv2.drawContours(crop_img, contours, -1, (0,255,0), 1)
print "cx:" + str(cx)
print "cy:" + str(cy)
print ""
if cx >= 120:
GPIO.output(Forward2, GPIO.HIGH)
GPIO.output(Forward, GPIO.LOW)
if cx < 120 and cx > 50:
GPIO.output(Forward, GPIO.HIGH)
GPIO.output(Forward2, GPIO.HIGH)
if cx <= 50:
GPIO.output(Forward2, GPIO.LOW)
GPIO.output(Forward, GPIO.HIGH)
else:
GPIO.output(Forward, GPIO.LOW)
GPIO.output(Forward2, GPIO.LOW)
#Display the resulting frame
cv2.imshow('frame',crop_img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if __name__ == "__main__":
app.run('10.0.89.215', 9999)