the_electro_artist
Created February 20, 2023 © CERN-OHL2

Esp32 Cam Testing in Local Server

The ESP32-CAM is a very small camera module with the ESP32-S chip that costs approximately $10

AdvancedFull instructions provided2 hours8
Esp32 Cam Testing in Local Server

Things used in this project

Hardware components

ESP32S
Espressif ESP32S
×1
Mega-B Development Kit
Breadware Inc Mega-B Development Kit
×1
LED (generic)
LED (generic)
×1
9V battery (generic)
9V battery (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Tweezers, SMD Soldering/Desoldering
Tweezers, SMD Soldering/Desoldering
Soldering iron (generic)
Soldering iron (generic)
Solder Flux, Soldering
Solder Flux, Soldering
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

Schematics

Schematics

Code

Code

Python
code
import mediapipe as mp

import cv2

import numpy as np

import time

from playsound import playsound


cap = cv2.VideoCapture(0)

cPos = 0

startT = 0  

endT = 0

userSum = 0

dur = 0

isAlive = 1

isInit = False

cStart, cEnd = 0,0

isCinit = False

tempSum = 0

winner = 0

inFrame = 0

inFramecheck = False

thresh = 180


def calc_sum(landmarkList):


    tsum = 0

    for i in range(11, 33):

        tsum += (landmarkList[i].x * 480)


    return tsum


def calc_dist(landmarkList):

    return (landmarkList[28].y*640 - landmarkList[24].y*640)


def isVisible(landmarkList):

    if (landmarkList[28].visibility > 0.7) and (landmarkList[24].visibility > 0.7):

        return True

    return False


mp_pose = mp.solutions.pose

pose = mp_pose.Pose()

drawing = mp.solutions.drawing_utils


im1 = cv2.imread('im1.jpg')

im2 = cv2.imread('im2.jpg')


currWindow = im1


while True:


    _, frm = cap.read()

    rgb = cv2.cvtColor(frm, cv2.COLOR_BGR2RGB)

    res = pose.process(rgb)

    frm = cv2.blur(frm, (5,5))

    drawing.draw_landmarks(frm, res.pose_landmarks, mp_pose.POSE_CONNECTIONS)


    if not(inFramecheck):

        try:

            if isVisible(res.pose_landmarks.landmark):

                inFrame = 1

                inFramecheck = True

            else:

                inFrame = 0

        except:

            print("You are not visible at all")


    if inFrame == 1:

        if not(isInit):

            playsound('greenLight.mp3')

            currWindow = im1

            startT = time.time()

            endT = startT

            dur = np.random.randint(1, 5)

            isInit = True


        if (endT - startT) <= dur:

            try:

                m = calc_dist(res.pose_landmarks.landmark)

                if m < thresh:

                    cPos += 1


                print("current progress is : ", cPos)

            except:

                print("Not visible")


            endT = time.time()


        else:      


            if cPos >= 100:

                print("WINNER")

                winner = 1


            else:

                if not(isCinit):

                    isCinit = True

                    cStart = time.time()

                    cEnd = cStart

                    currWindow = im2

                    playsound('redLight.mp3')

                    userSum = calc_sum(res.pose_landmarks.landmark)


                if (cEnd - cStart) <= 3:

                    tempSum = calc_sum(res.pose_landmarks.landmark)

                    cEnd = time.time()

                    if abs(tempSum - userSum) > 150:

                        print("DEAD ", abs(tempSum - userSum))

                        isAlive = 0


                else:

                    isInit = False

                    isCinit = False


        cv2.circle(currWindow, ((55 + 6*cPos),280), 15, (0,0,255), -1)


        mainWin = np.concatenate((cv2.resize(frm, (800,400)), currWindow), axis=0)

        cv2.imshow("Main Window", mainWin)

        #cv2.imshow("window", frm)

        #cv2.imshow("light", currWindow)


    else:

        cv2.putText(frm, "Please Make sure you are fully in frame", (20,200), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,255,0), 4)

        cv2.imshow("window", frm)


    if cv2.waitKey(1) == 27 or isAlive == 0 or winner == 1:

        cv2.destroyAllWindows()

        cap.release()

        break



Add TipAsk QuestionCommentDownload
Step 4: Wiring
Wiring
Wiring
To detect the object the python code uses a step-by-step method of image or frame conversion. At first, it tries to convert the RGB image to Grayscale such that the difference in magnitude of color is greatly visible and mathematical operations can be performed easily. After that to blend in the colors, we blur the image, now the canny edge detection takes place where it easily detects the edges, thus now to properly join the edges detected we dilate the image. In the end, we retrieve the number of closed figures after detecting edges.



import cv2

import urllib.request

import numpy as np


url='http://192.168.1.61/'

##'''cam.bmp / cam-lo.jpg /cam-hi.jpg / cam.mjpeg '''

cv2.namedWindow("live transmission", cv2.WINDOW_AUTOSIZE)



while True:

    img_resp=urllib.request.urlopen(url+'cam-lo.jpg')

    imgnp=np.array(bytearray(img_resp.read()),dtype=np.uint8)

    img=cv2.imdecode(imgnp,-1)


    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    canny=cv2.Canny(cv2.GaussianBlur(gray,(11,11),0),30,150,3)

    dilated=cv2.dilate(canny,(1,1),iterations=2)

    (Cnt,_)=cv2.findContours(dilated.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

    k=img

    cv2.drawContours(k,cnt,-1,(0,255,0),2)


    cv2.imshow("mit contour",canny)

    cv2.imshow("live transmission",img)

    key=cv2.waitKey(5)

    if key==ord('q'):

        break

    elif key==ord('a'):

        cow=len(cnt)

        print(cow)

cv2.destroyAllWindows()

Credits

the_electro_artist
30 projects • 3 followers

Comments