본문 바로가기
project/2020.12-02 (라즈베리파이) 눈으로운전하는휠체어

if x<80 : print('right') elif x>300 : print('left')

by sj0020 2021. 1. 20.
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()

 

 

https://icbanq.tistory.com/entry/%EB%9D%BC%EC%A6%88%EB%B2%A0%EB%A6%AC%ED%8C%8C%EC%9D%B4-RC%EC%B9%B4-%EB%A7%8C%EB%93%A4%EA%B8%B0

 

[라즈베리파이] RC카 만들기

RC Car on the Raspberrypi RC카를 제작시에 가장 고민이 되는 부분이 RC카의 외형이기에 기존 판매 중인 제품 중 저렴 한 리모컨용 RC카(3만원-5만원)를 구매 해 내부를 들어내고 라즈베리파이용 RC카로

icbanq.tistory.com