Giter Site home page Giter Site logo

sefakcmn00 / servo-motor-control-in-ardunio-using-handcascade-tutorial-file-in-opencv Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 1.0 13 KB

It is the Servo motor control project of our Computer Vision work with Opencv. In this project, an Ardunio Serial port-based servo motor control project was implemented using Opencv and Cvzone artificial intelligence libraries. Below I tried to explain the details of the project step by step.

Python 15.42% C++ 84.58%
ardunio computer-vision cvzone opencv python servo-motor handcascade

servo-motor-control-in-ardunio-using-handcascade-tutorial-file-in-opencv's Introduction


Servo-motor-control-in-Ardunio-using-HandCascade-tutorial-file-in-Opencv

It is the Servo motor control project of our Computer Vision work with Opencv. In this project, an Ardunio Serial port-based servo motor control project was implemented using Opencv and Cvzone artificial intelligence libraries. Below I tried to explain the details of the project step by step.

Download Library

First, we will download the libraries we will use.

import cv2
from cvzone.HandTrackingModule import HandDetector
import numpy as np
import pyfirmata
import random

Code block that we connect to Arduino

port = "COM5"
board = pyfirmata.Arduino(port)
servo_pinX = board.get_pin('d:5:s') #pin 5 Arduino
servo_pinY = board.get_pin('d:6:s') #pin 6 Arduino

We add the StandardFirmata project from the Arduino sample Firmata library. It is the library that allows Arduino to communicate with python.

arduion

We add the StandardFirmata project from the Arduino sample Firmata library. It is the library that allows Arduino to communicate with python.

image

Python Code Blog

cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)

detector = HandDetector(detectionCon=0.8, maxHands=2)

port = "COM5"
board = pyfirmata.Arduino(port)
servo_pinX = board.get_pin('d:5:s') #pin 5 Arduino
servo_pinY = board.get_pin('d:6:s') #pin 6 Arduino

posCircle = []
posCircleTarget = []

minHand, maxHand = 20, 220
minDeg, maxDeg = 0, 180
minBar, maxBar = 400, 150
xbox, ybox = 400, 500
wbox, hbox = 450, 450
x_rand = random.randint(xbox + 25, xbox + wbox - 25)
y_rand = random.randint(ybox - hbox + 25, ybox - 25)
score = 0
edgeBox = False

while True:
    success, img = cap.read()
    hands = detector.findHands(img, draw = False)


    if hands:

        hand_l = hands[0]
        lmList_l = hand_l["lmList"]  # List of 21 Landmark points
        length_l, info_l, img = detector.findDistance(lmList_l[8][0:2], lmList_l[4][0:2], img)


        # Hand 2 (Right)
        if len(hands) == 2:
            hand_r = hands[1]
            lmList_r = hand_r["lmList"]  # List of 21 Landmark points
            length_r, info_r, img = detector.findDistance(lmList_r[8][0:2], lmList_r[4][0:2], img)  # with draw


            xl = lmList_l[8][0]
            yl = lmList_l[8][1]
            xr = lmList_r[8][0]
            yr = lmList_r[8][1]

            # left x | right y
            circleX = np.interp(length_l, [minHand, maxHand], [xbox+25, xbox+wbox-25])
            circleY = np.interp(length_r, [minHand, maxHand], [ybox-25, ybox-hbox+25])

            servoX = np.interp(length_l, [minHand, maxHand], [minDeg, maxDeg])
            servoY = np.interp(length_r, [minHand, maxHand], [maxDeg, minDeg])

            barX = np.interp(length_l, [minHand, maxHand], [minBar, maxBar])
            barY = np.interp(length_r, [minHand, maxHand], [minBar, maxBar])

            # circle player
            posCircle = [int(circleX), int(circleY)]
            cv2.circle(img, posCircle, 25, (0, 0, 255), cv2.FILLED )

            if xbox+25 < posCircle[0] < xbox+wbox-25 and ybox-hbox+25 < posCircle[1] < hbox+25:
                colBox = [255, 0, 0]
                if edgeBox == False:
                    edgeBox = not edgeBox
            else:
                colBox = [0, 0, 255]
                if edgeBox:
                    score = 0
                    edgeBox = not edgeBox


            # box area
            cv2.rectangle(img, (xbox, ybox+50), (xbox+wbox, ybox-hbox), colBox, 3)
            cv2.rectangle(img, (xbox, ybox), (xbox+wbox, ybox+50), colBox, cv2.FILLED)
            cv2.putText(img, f'Score : {score}', (xbox+80, ybox+40), cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 3)

            #circle target
            if x_rand-35 < posCircle[0] < x_rand+35 and y_rand-35 < posCircle[1] < y_rand+35   :
                x_rand = random.randint(xbox + 25, xbox + wbox - 25)
                y_rand = random.randint(ybox - hbox + 25, ybox - 25)
                score += 5
            cv2.circle(img, (x_rand, y_rand), 25, (0, 255, 255), cv2.FILLED)

            #bar
            cv2.rectangle(img, (1180, 150), (1215, 400), (255, 0, 0), 3)
            cv2.rectangle(img, (1180, int(barX)), (1215, 400), (0, 255, 0), cv2.FILLED)

            cv2.rectangle(img, (50, 150), (85, 400), (255, 0, 0), 3)
            cv2.rectangle(img, (50, int(barY)), (85, 400), (0, 255, 0), cv2.FILLED)

            # servo control
            servo_pinX.write(servoX)
            servo_pinY.write(servoY)

    cv2.imshow("Image", img)
    if cv2.waitKey(1) & 0xFF==ord('q'):
        break

Project Output

servo

servo-motor-control-in-ardunio-using-handcascade-tutorial-file-in-opencv's People

Contributors

sefakcmn00 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

naturevision100

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.