import cv2
import numpy as np
cap = cv2.VideoCapture("1eun2.mp4")
while True:
ret, frame = cap.read()
if ret is False:
break
roi = frame[100: 795, 700: 1300]
rows, cols, _ = roi.shape
gray_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
gray_roi = cv2.GaussianBlur(gray_roi, (7, 7), 0)
_, threshold = cv2.threshold(gray_roi, 30, 255, cv2.THRESH_BINARY_INV)
_, contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
cv2.drawContours(roi, [cnt], -1, (0, 0, 255), 3)
cv2.rectangle(roi, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.line(roi, (x + int(w/2), 0), (x + int(w/2), rows), (0, 255, 0), 2)
cv2.line(roi, (0, y + int(h/2)), (cols, y + int(h/2)), (0, 255, 0), 2)
break
print('x:', x , 'y:', y)
cv2.imshow("Threshold", threshold) #
cv2.imshow("gray roi", gray_roi) # 흑백
cv2.imshow("Roi", roi) #원본
key = cv2.waitKey(30)
if key == 27:
break
if x<80 :
print('right')
elif x>300 :
print('left')
cv2.destroyAllWindows()
import cv2
import numpy as np
from gpiozero import Robot
from gpiozero import Motor
import time
dc_motor = Robot(left=(12, 16), right=(20, 21))
dc_left = Robot(left=(12, 16))
dc_right = Robot(right(20,21))
cap = cv2.VideoCapture("1eun2.mp4")
while True:
ret, frame = cap.read()
if ret is False:
break
roi = frame[100: 795, 700: 1300]
rows, cols, _ = roi.shape
gray_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
gray_roi = cv2.GaussianBlur(gray_roi, (7, 7), 0)
_, threshold = cv2.threshold(gray_roi, 30, 255, cv2.THRESH_BINARY_INV)
_, contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
cv2.drawContours(roi, [cnt], -1, (0, 0, 255), 3)
cv2.rectangle(roi, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.line(roi, (x + int(w/2), 0), (x + int(w/2), rows), (0, 255, 0), 2)
cv2.line(roi, (0, y + int(h/2)), (cols, y + int(h/2)), (0, 255, 0), 2)
break
#print('x:', x , 'y:', y)
print(y)
# x, y 는 왼쪽 상단을 기준으로 측정
# x = 100 right, x = 300 left, y = 330 forward, y = 500 backward
cv2.imshow("Threshold", threshold) #
cv2.imshow("gray roi", gray_roi) # 흑백
cv2.imshow("Roi", roi) #원본
key = cv2.waitKey(30)
if key == 27:
break
"""if x<100 :
print('right')
dc_left.forward(speed=1)
dc_right.forward(speed=0.1)
elif x>300 :
print('left')
dc_right.forward(speed=1)
dc_left.forward(speed=0.1)
elif y < 330 :
print("FORWARD")
dc_motor.forward(speed=1)
#time.sleep(3)
elif y > 500 :
print("BACKWARD")
dc_motor.backward(speed=1)
#time.sleep(3)
else :
print("stop ")
dc_motor.stop()
#time.sleep(0.5)"""
cv2.destroyAllWindows()
'project > 2020.12-02 (라즈베리파이) 눈으로운전하는휠체어' 카테고리의 다른 글
dc모터 모터드라이브 사용해 2개 + 2개 동시 제어 (0) | 2021.01.21 |
---|---|
바퀴 (0) | 2021.01.20 |
해석 (0) | 2021.01.20 |
오늘작업 (0) | 2021.01.19 |
0eun, 주석 포함 코드, 주석 삭제한 코드 (0) | 2021.01.19 |