-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths2.py
142 lines (124 loc) · 3.37 KB
/
s2.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
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
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 14 20:18:04 2020
@author: ohtak
"""
gpio_pin = 17
gpio_pin2 = 27
#1= left
#2= right
import sys
import time
import cv2
import numpy as np
import RPi.GPIO as GPIO
cap = cv2.VideoCapture(0)
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio_pin , GPIO.OUT)
GPIO.setup(gpio_pin2 , GPIO.OUT)
#27 = right
run_time = 0.1
def Right(none):
GPIO.output(gpio_pin, 0)
GPIO.output(gpio_pin2, 1)
time.sleep(run_time)
Off("a")
return 0
def Left(none):
GPIO.output(gpio_pin, 1)
GPIO.output(gpio_pin2, 0)
time.sleep(run_time)
Off("a")
return 0
def Straight(none):
GPIO.output(gpio_pin, 1)
GPIO.output(gpio_pin2, 1)
time.sleep(run_time + 0.5)
Off("a")
return 0
def Off(which):
if which == "r":
GPIO.output(gpio_pin2 , 0)
elif which == "l":
GPIO.output(gpio_pin , 0)
elif which == "a":
GPIO.output(gpio_pin , 0)
GPIO.output(gpio_pin2 , 0)
return 0
before = None
camera_mode = 3
while True:
try:
ret, frame = cap.read()
if camera_mode == 2:
cv2.imshow("camera", frame)
else:
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
if camera_mode == 1:
if before is None:
before = gray.copy().astype("float")
continue
cv2.imshow("camera", gray)
else:
#before = gray.copy().astype("float")
#ret,two = cv2.threshold(gray,147,255,cv2.THRESH_BINARY)
ret,two = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
cv2.imshow("camera", two)
"""
cv2.accumulateWeighted(gray, before, 0.7)
mdframe = cv2.absdiff(gray, cv2.convertScaleAbs(before))
ret,thresh2 = cv2.threshold(mdframe,10,255,cv2.THRESH_BINARY)
ret,thresh1 = cv2.threshold(gray,147,255,cv2.THRESH_BINARY)
cv2.imshow("threshed1", thresh1)
"""
k = cv2.waitKey(1)&0xff
if k == ord("w"):
print("w")
Straight(0)
elif k == ord("d"):
print("r")
Right(0)
elif k == ord("a"):
print("l")
Left(0)
elif k == ord("i"):
run_time += 0.1
print(run_time)
print("Straight" , str(run_time + 0.5))
elif k == ord("k"):
run_time -= 0.1
print(run_time)
print("Straight" , str(run_time + 0.5))
elif k == ord("o"):
if camera_mode < 3:
camera_mode += 1
print("camera_mode set "+ str(camera_mode) + ".")
elif k == ord("l"):
if camera_mode > 1:
camera_mode -= 1
print("camera_mode set "+ str(camera_mode) + ".")
elif k == 27:
break
"""
answer = ""
answer = raw_input("Chose, s or r or l or ss or e.")
if answer == "s":
Straight(0)
elif answer == "r":
Right(0)
elif answer == "l":
Left(0)
elif answer == "ss":
Off("a")
elif answer == "e":
break
else:
print("What?")
"""
except KeyboardInterrupt:
print("safe end.")
break
cv2.destroyAllWindows()
cap.release
GPIO.cleanup()
print("All end.")