Here is my python code:
import cv2
import cvzone.SerialModule
from cvzone.SerialModule import SerialObject
from cvzone.HandTrackingModule import HandDetector
import serial
cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands = 1, detectionCon = 0.7)
mySerial = SerialObject("COM7", 9600, 1)
while True:
success, img = cap.read()
# The 'flipType' parameter flips the image, making it easier for some detections
hands, img = detector.findHands(img, flipType=True)
if hands:
# Gets fingers for the first detected hand
fingers = detector.fingersUp(hands[0])
print(fingers)
mySerial.sendData(fingers)
cv2.imshow("Image", img)
cv2.waitKey(1)
and this is the arduino code:
#include <Servo.h>
#define numOfValsRec 5
#define digitsPerValRec 1
Servo servoThumb;
Servo servoIndex;
Servo servoMiddle;
Servo servoRing;
Servo servoPinky;
int valsRec[numOfValsRec];
int stringLength = numOfValsRec * digitsPerValRec +1;
int counter = 0;
bool counterStart = false;
String recievedString;
void setup() {
Serial.begin(9600);
servoThumb.attach(8);
servoIndex.attach(9);
servoMiddle.attach(10);
servoRing.attach(11);
servoPinky.attach(12);
}
void receiveData() {
while(Serial.available()) {
char c = Serial.read();
if (c=='$'){
counterStart = true;
}
if (counterStart){
if (counter < stringLength) {
recievedString = String(recievedString+c);
counter++;
}
if (counter>= stringLength){
for(int i = 0; i<numOfValsRec; i++)
{
int num = (i*digitsPerValRec)+1;
valsRec[i] = recievedString.substring(num,num + digitsPerValRec).toInt();
}
recievedString = "";
counter = 0;
counterStart = false;
}
}
}
}
void loop(){
receiveData();
if (valsRec[0] == 1) {servoThumb.write(180);}else{servoThumb.write(0);}
if (valsRec[1] == 1) {servoIndex.write(180);}else{servoIndex.write(0);}
if (valsRec[2] == 1) {servoMiddle.write(180);}else{servoMiddle.write(0);}
if (valsRec[3] == 1) {servoRing.write(180);}else{servoRing.write(0);}
if (valsRec[4] == 1) {servoPinky.write(180);}else{servoPinky.write(0);} `
}
This is the error:
Attempt 1 of 5 to connect...
Attempt 1 failed. Retrying...
Attempt 2 of 5 to connect...
Attempt 2 failed. Retrying...
Attempt 3 of 5 to connect...
Attempt 3 failed. Retrying...
Attempt 4 of 5 to connect...
Attempt 4 failed. Retrying...
Attempt 5 of 5 to connect...
Attempt 5 failed. Retrying...
WARNING:root:Serial Device Not Connected. Max retries reached.
I basically 3d-printed a hand and when i move my hand in front of a camera, the hand moves accordingly. I need to sort this out for my exhibition, I have done everything like checking COM port, file name, installing just pyserial. Please help. If i just run my arduino and python code it works, the problem is in the communication.