我用arduino+SR04传感器连接了我的笔记本电脑。我想做的是,一旦距离传感器检测到笔记本电脑附近有人(距离读数小于50厘米),我就想使用python opencv打开网络摄像头(稍后保存此视频剪辑) 我面临的问题是arduino+SR04和python之间的通信不同步。即使在人员离开后,phython控制台上的传感器读数仍会持续弹出约3-4秒(这可能是由于传感器数据写入速度不如摄像头显示速度快)。这么长时间的延迟,我的视频会占用很多空间
我尝试将SR04传感器的延迟增加到500毫秒,但这使得网络摄像头变得非常缓慢。我不想让视频有起伏。请帮忙
我的arduino+SR04代码
const int echo_pin = 9;
const int trig_pin = 10;
void setup() {
Serial.begin(9600);
pinMode(echo_pin, INPUT);
pinMode(trig_pin, OUTPUT);
}
void loop() {
double period, distance;
digitalWrite(trig_pin, LOW);
delayMicroseconds(2);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
period = pulseIn(echo_pin, HIGH);
distance = (period / 2) / 29.1;
//delay(500);
Serial.println(distance);
}
我的Python代码
import serial
import time
import cv2
cap = cv2.VideoCapture(0)
arduino = serial.Serial('COM10', 9600)
def Measure():
distance = arduino.readline()
#time.sleep(.5)
print ("distance "+str(distance)+" cm")
return float(distance)
while True:
output = Measure()
if output < 50:
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
目前没有回答
相关问题 更多 >
编程相关推荐