私はPythonでこのスクリプトを書いて、Arduino UNOにシリアル通信でデータを送信しました。 OpenCVライブラリを使用してバイナリイメージを処理し、検出されたオブジェクトの位置に基づいて決定を行います。条件文が正しく動作しない
はここにスクリプトです: -
import numpy as np
import cv2
import serial
ser = serial.Serial('COM3',9600,writeTimeout = 0)
def f(x): return
cv2.namedWindow('Thresholding Control')
# create trackbars for color change
cv2.createTrackbar('High H','Thresholding Control',179,179, f)
cv2.createTrackbar('Low H','Thresholding Control',0,179, f)
cv2.createTrackbar('High S','Thresholding Control',255,255, f)
cv2.createTrackbar('Low S','Thresholding Control',0,255, f)
cv2.createTrackbar('High V','Thresholding Control',255,255, f)
cv2.createTrackbar('Low V','Thresholding Control',0,255, f)
cv2.createTrackbar('Guassian Blur','Thresholding Control',0,99, f)
cap = cv2.VideoCapture(0)
while(True):
ret, image = cap.read()
HSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Getting trackbar values
highH = cv2.getTrackbarPos('High H','Thresholding Control')
lowH = cv2.getTrackbarPos('Low H','Thresholding Control')
highS = cv2.getTrackbarPos('High S','Thresholding Control')
lowS = cv2.getTrackbarPos('Low S','Thresholding Control')
highV = cv2.getTrackbarPos('High V','Thresholding Control')
lowV = cv2.getTrackbarPos('Low V','Thresholding Control')
# Thresholding the image.
thresh = cv2.inRange(HSV, (lowH, lowS, lowV), (highH, highS, highV))
blurVal = cv2.getTrackbarPos('Guassian Blur','Thresholding Control')
if(blurVal%2==0):
blurVal=blurVal+1
thresh_smooth = cv2.GaussianBlur(thresh, (blurVal, blurVal), 0)
#Defining the kernel to be used for Morphological ops.
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
# Applying Opening and Closing.
thresh_smooth = cv2.morphologyEx(thresh_smooth,cv2.MORPH_OPEN,kernel)
thresh_smooth = cv2.morphologyEx(thresh_smooth, cv2.MORPH_CLOSE, kernel)
eleR = np.count_nonzero(thresh_smooth[0:480, 320:550])
eleL = np.count_nonzero(thresh_smooth[0:480, 0:320])
eleO = np.count_nonzero(thresh_smooth[0:480, 550:640])
if (eleL>eleR and eleL>eleO and eleL!= (eleR+eleO)):
cv2.putText(image,"Left Turn", (320,240), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
ser.write('L')
if (eleO>eleR and eleO>eleL):
cv2.putText(image,"Right Turn", (240,320), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
ser.write('R')
else:
cv2.putText(image,"Straight", (240,320), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
ser.write('S')
cv2.imshow("BGR", image)
cv2.imshow("Thresholded", thresh_smooth)
print ser.readline();
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
ここでの問題はif
文です。 3つの条件のうち、どちらの条件が真であっても、print ser.readline()
コマンドで確認されているように、値「S」(else
ステートメント内)のみがシリアル経由で送信されます。すると、画像内のどの位置にあっても、ブロックの値が突然現れ、同じ値がそのまま続くことがあります。 cv2.putText
コマンドは正常に動作しますが、問題の原因となっているのはser.write
のみです。
if
の代わりにelif
を入れてみましたが、何らかの理由で実行時にスクリプトをクラッシュさせるcontinue
ステートメントをそれぞれの条件ブロックに入れてみました。彼らの誰も助けなかった。私はこのバグをどのように修正するのか分かりません。
ありがとうございました。
UPDATE 115200にボーレートを増加しようとしました
、動作しませんでした。 RESETピンとGNDピンの間にコンデンサを挿入しても機能しませんでした。
スイートの前に、 'ele0、eleR'と' eleL'を表示してください。あなたのテストが合理的であることを確認してください。 –
@joelgoldstick私はいくつかの値を得ました。彼らは意味をなす。 –
申し訳ありませんが、私はいくつかを逃しました。コメントを削除しました –