if results.multi_hand_landmarks: # if this statement is true, enter the for loop
ic = '1'
command = ic
serialInst.write(command.encode('utf-8'))
The pyscript is a mediapipe code. the serial prints fine
on my C side i have this in my loop
if (Serial.available() > 0) {
String(msg) = Serial.readString();
Serial.println("Message:");
Serial.println(msg);
i noticed the serial monitor only prints like this Message:"0000000000000011111111111111111"
while i was expecting it to print like
Message:0
Message:1
Message:0
which is like the big issue because i need the message to be split so the controls .
int ledpin = 2;
int buzzer = 3;
int n = 1;
void setup() {
Serial.begin(9600);
pinMode(ledpin, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
String(msg) = Serial.readString();
Serial.println("Message:");
Serial.println(msg);
digitalWrite(ledpin, HIGH);
if (msg == "1") {
digitalWrite(buzzer, HIGH);
}
else if (msg == "0") {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
}
else {
digitalWrite(ledpin, LOW);
}
}
delay(500);
}
C Code
import cv2
import mediapipe as mp
import time
import serial.tools.list_ports
webcam = cv2.VideoCapture(0)
mpHands = mp.solutions.hands #Default set code for hand tracking. Ritual Required
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils
ports = serial.tools.list_ports.comports() #Listing available ports
serialInst = serial.Serial() #created instance
portsList = [] # variable to hold port list
for onePort in ports:
portsList.append(str(onePort))
print(str(onePort))
val = input("Select Port: COM")
for x in range(0,len(portsList)):
if portsList[x].startswith("/dev/ttyACM" + str(val)): #on windows the port starts with the 'com' sequence // ports on linux follow the format '/dev/ttyACM'
portVar = "/dev/ttyACM" + str(val)
print(portVar)
serialInst.baudrate = 9600
serialInst.port = portVar
serialInst.open()
while True:
ret, frame = webcam.read()
imguse = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Images used by mediapipe are in RGB color spaces
results = hands.process(imguse)
print (results.multi_hand_landmarks) # getting the exact hand landmark location
if results.multi_hand_landmarks: # if this statement is true, enter the for loop
ic = '1'
command = ic
serialInst.write(command.encode('utf-8'))
for handLms in results.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark): # without handLms we can directly enumerate results.multi_hand_landmarks
h,w,c = frame.shape
cx, cy = int (lm.x * w), int (lm.y * h)
print (id, cx, cy)
mpDraw.draw_landmarks (frame, handLms, mpHands.HAND_CONNECTIONS)
#set mpDraw outside the loop as usual. The parameters are (what display image, how many hands Hand LMS means single hand, connection line between dots )
else :
ic = '0'
command = ic
serialInst.write(command.encode('utf-8'))
for handLms in results.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark): # without handLms we can directly enumerate results.multi_hand_landmarks
h,w,c = frame.shape
cx, cy = int (lm.x * w), int (lm.y * h)
print (id, cx, cy)
mpDraw.draw_landmarks (frame, handLms, mpHands.HAND_CONNECTIONS)
cv2.imshow('frame', frame)
if cv2.waitKey(25) & 0xFF == ord('x'):
cv2.destroyAllWindows()
break
webcam.release()
cv2.destroyAllWindows()
Py COde