'Get data from Arduino and displaying video with cv2 - Python
I am working on a project where I get the roll data from the accelerometer in Arduino and then group the rotation in terms of 1 and 0, 1 means that the previous roll value is different from the updated roll value, and 0 means that it is the same as the previous. So if it displays 1 the video will appear and if 0 the video will pause. However, I am not sure how to go about it and faced issues in making the video play and pause. Would greatly appreciate your help thankyou!!
Python code
import cv2
import serial
try:
arduino=serial.Serial("COM5", timeout=1)
except:
print("Check port")
def whileloop():
rawdata=[]
decode_data=[]
while True:
decode_data.append(arduino.read())
for i in decode_data:
rawdata.append(i.decode("utf-8"))
print(rawdata)
if len(rawdata)==10:
if rawdata.count("1")>=1:
cap=cv2.VideoCapture("test.mp4.mp4")
while(cap.isOpened()):
ret,frame=cap.read()
cv2.imshow("video",frame)
if cv2.waitKey(10)==ord('q'):
whileloop()
elif rawdata.count("0")>rawdata.count("1"):
cv2.waitKey(10)
break
else:
whileloop()
rawdata=[]
whileloop()
whileloop()
Arduino Code
int x,y,z,roll;
int prev = 0;
const int relay_3v3_pin=3;
void setup() {
pinMode(relay_3v3_pin,OUTPUT);
Serial.begin(9600);
}
void loop() {
x = analogRead(A0);
y = analogRead(A1);
z = analogRead(A2);
/*Serial.print("acceletation in x, y, z direction: ");
Serial.print(x);
Serial.print(" ");
Serial.print(y);
Serial.print(" ");
Serial.println(z);*/
if (roll==prev)
{ digitalWrite(relay_3v3_pin, LOW);
delay(300);roll = ( ( (atan2(y,z) * 180) / 3.14 ) + 180 ); /* Formula for roll */
} else {
prev=roll;
digitalWrite(relay_3v3_pin, HIGH);
delay(100);
}
if (roll==prev)
{ digitalWrite(relay_3v3_pin, LOW);
Serial.print(0);
delay(50);
} else {
digitalWrite(relay_3v3_pin, HIGH);
Serial.print(1);
prev==roll;
}
delay(200);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
