ProjectsProjeler / CV-02
Computer vision · serial link · actuatorGörüntü işleme · seri haberleşme · eyleyici
Hand gesture → servoEl hareketi → servo
A webcam, a Python script and an Arduino. OpenCV finds my hand in each frame, the script turns its horizontal position into an angle, and the Arduino moves a servo to that angle after reading it from the serial port.
Bir kamera, bir Python scripti ve bir Arduino. OpenCV her karede elimi buluyor, script elin yatay konumunu bir açıya çeviriyor, Arduino da bu açıyı seri porttan okuyup servoyu o açıya getiriyor.
How it works
The project has two halves that only share one thing: a number sent over a USB serial cable.
PC side
Python reads frames from the webcam with OpenCV and finds the hand in the image. From the hand's position the script takes the x coordinate of its centre and maps it linearly to the servo range:
angle = int(x / frame_width * 180) # 0 … 180 degrees
ser.write(f"{angle}\n".encode())
Each angle is sent as a short text line ending in a newline, so the Arduino always knows where one value stops and the next one starts.
Board side
The Arduino reads characters until the newline, converts them to an integer, limits it to 0–180 and writes it to the servo. A hobby servo expects a pulse every 20 ms (50 Hz), and the pulse width, roughly 1 ms to 2 ms, sets the angle. The Servo library generates that PWM signal from a timer.
Where the time went
The vision code was the visible part, but a big share of the work was on the physical side: stabilising the breadboard, redoing the wiring until nothing moved, and making sure the signal reached the servo continuously and in the right order. A servo that twitches is usually a wiring or power problem, not a code problem, and finding that out took a multimeter more than a debugger.
Concepts used
Hand detection in an image stream, linear coordinate mapping, UART framing with a delimiter, PWM servo control, shared ground between power domains. Short explanations are on the notes page.
Nasıl çalışıyor
Projenin, ortak tek bir şeyi olan iki yarısı var: USB seri kablo üzerinden gönderilen bir sayı.
PC tarafı
Python, OpenCV ile kameradan kareleri okuyor ve görüntüde eli buluyor. Script elin konumundan merkezinin x koordinatını alıyor ve bunu doğrusal olarak servo aralığına eşliyor:
angle = int(x / frame_width * 180) # 0 … 180 derece
ser.write(f"{angle}\n".encode())
Her açı, satır sonu karakteriyle biten kısa bir metin satırı olarak gönderiliyor; böylece Arduino bir değerin nerede bitip diğerinin nerede başladığını her zaman biliyor.
Kart tarafı
Arduino satır sonuna kadar karakterleri okuyor, bunları tam sayıya çeviriyor, 0–180 aralığına sınırlıyor ve servoya yazıyor. Hobi servoları her 20 ms'de (50 Hz) bir darbe bekliyor; yaklaşık 1 ms ile 2 ms arasındaki darbe genişliği açıyı belirliyor. Bu PWM sinyalini Servo kütüphanesi bir zamanlayıcı ile üretiyor.
Zamanın çoğu nereye gitti
Görünen kısım görüntü işleme koduydu ama işin büyük bir kısmı fiziksel taraftaydı: breadboard'u sabitlemek, hiçbir şey oynamayana kadar kabloları yeniden yapmak ve sinyalin servoya kesintisiz ve doğru sırayla ulaştığından emin olmak. Titreyen bir servo genelde kod değil, kablolama ya da besleme sorunudur; bunu bulmak da debugger'dan çok multimetre gerektirdi.
Kullanılan kavramlar
Görüntü akışında el algılama, doğrusal koordinat eşleme, ayraçla UART çerçeveleme, PWM ile servo kontrolü, güç kaynakları arasında ortak toprak. Kısa açıklamalar notlar sayfasında.