ML Project

Sonam Thakur
10 min readMay 21, 2023

--

๐‘ฌ๐’™๐’„๐’Š๐’•๐’Š๐’๐’ˆ ๐‘ท๐’“๐’๐’‹๐’†๐’„๐’• : ๐‘ซ๐’๐’„๐’Œ๐’†๐’“ ๐‘ช๐’๐’๐’•๐’‚๐’Š๐’๐’†๐’“๐’” ๐’˜๐’Š๐’•๐’‰ ๐‘ญ๐’Š๐’๐’ˆ๐’†๐’“๐’”! ๐Ÿ–๐Ÿš€

๐˜๐˜ฎ๐˜ข๐˜จ๐˜ช๐˜ฏ๐˜ฆ that launch ๐˜‹๐˜ฐ๐˜ค๐˜ฌ๐˜ฆ๐˜ณ ๐˜ค๐˜ฐ๐˜ฏ๐˜ต๐˜ข๐˜ช๐˜ฏ๐˜ฆ๐˜ณ๐˜ด ๐˜ธ๐˜ช๐˜ต๐˜ฉ ๐˜ซ๐˜ถ๐˜ด๐˜ต ๐˜ข ๐˜ธ๐˜ข๐˜ท๐˜ฆ ๐˜ฐ๐˜ง ๐˜บ๐˜ฐ๐˜ถ๐˜ณ ๐˜ฉ๐˜ข๐˜ฏ๐˜ฅ! ๐ŸŒŸ

Using advanced finger detection techniques and leveraging the power of AWS services, Iโ€™ve developed a solution that allows you to scale your Docker containers on EC2 Linux instances with a simple gesture.
explain my project in brief: -
๐Ÿ”บCamera click the photo and detect hand landmark.
๐Ÿ”บ Detect the hand and give result in variables.
๐Ÿ”บ continue photo work as video live streaming and the result of fingers in [0,1] from.
๐Ÿ”บAdd some features in finger detection
According to my hand gesture detection it shows user locked or unlocked.
๐Ÿ”บ launched docker containers with hand gesture.
๐Ÿ”บAs i showed my fingers
its start launching OS.
With the help of aws, ec2, lambda ,ssh, and such use cases of aws services.

I'm going to build A project based on ML integration to AWS cloud for administrator work.

The problem is to scale out the docker container just by showing fingers. The fingers up indicate the number of new containers that are needed to be launched.

Launching EC2 Instance

We know, every service in AWS is an individual service.
For connecting EC2 to SSM, We need to use IAM roles.

Go to the IAM dashboard, then go to Roles using the Navigator pane on the left side.

  1. Create a role.
  2. Select as shown (Search for SSM and select the policy for full access to SSM).
  3. Set the name of the Role and create.
  4. we can the role created.
  5. Now, Launch an EC2 instance.
    I am using Amazon Linux 2 for this demonstration.
  6. Keep the rest as defaults, just make changes like the following.
  7. Proceed without any key pair.
  8. The Instance is launched.
  9. Now letโ€™s configure SSM to manage the ec2 manually first, then we will automate it also.

Come to AWS SYSTEM MANAGER Dashboard and go to Run Command from the navigation pane.

Click Run Command. Search for shell and select AWS-RunShellScript.then Scroll down and give the command for testing.Then scroll down and select as shown below.Scroll down, keep the rest as defaults, and Click on Run.The command was run successfully by SSM.

Configure docker service in Linux server.

Note: Itโ€™s not SSM that comes to the EC2 instance and configures the instance, EC2 comes to SSM and registers itself.
Due to being two independent services, EC2 needs the power to come to SSM. So, attach the IAM role to the EC2 instance and give full access to SSM.

Now run these commands.

#first  login as root 

# install docker service
yum install docker -y

# check the status of docker service

systemctl status docker

# start the service and enable on boot
systemctl start docker
systemctl enable docker

# pull container image
docker pull ubuntu:14.04

# you can use any other image.

Create a Lambda function.

We are using Lambda as a serverless service that connects to SSM and tell to run the necessary commands.

Write the code and deploy it.

Letโ€™s understand this code in brief.

  1. Import the required modules โ€˜jsonโ€™ and โ€˜boto3โ€™.
  2. Define a function โ€˜lambda_handlerโ€™ that takes two parameters: โ€˜eventโ€™ and โ€˜contextโ€™. โ€˜eventโ€™ contains information about the event that triggered the function, while โ€˜contextโ€™ provides runtime information about the function.
  3. Configure Lamda as a client of SSM (Systems Manager) using the โ€˜boto3โ€™ module.
    ssm_client = boto3.client(โ€˜ssmโ€™, region_name=โ€™ap-south-1')
  4. Create a command to be executed on an EC2 instance using the โ€˜send_commandโ€™ method of the SSM client.
    First define the command and put it in a variable for further usage.
    cmd = { โ€œcommandsโ€: [ โ€œdocker run -dit ubuntu:14.04โ€ ] }

    The command is like a parameter, what command you want to run, store the command in the variable,
    ssm_client.send_command(DocumentName=โ€AWS-RunShellScriptโ€, InstanceIds=[โ€œi-043cd8dfb03183120โ€], Parameters=cmd )
    Here, the command is to create a docker container from the ubuntu container image, on the EC2 instance with the ID โ€˜i-043cd8dfb03183120โ€™.
  5. Return a response with a status code of 200 and a message in JSON format saying โ€˜Docker Launched!โ€™
    return {
    โ€˜statusCodeโ€™: 200,
    โ€˜bodyโ€™: json.dumps(โ€˜Docker Launched!โ€™)
    }

NOTE: Donโ€™t forget create the IAM role for Lambda as Lambda and SSM are independent services. Here, Lambda is going to trigger SSM and tell to launch the docker containers.

If one role is created by default, then attach the respective policies.

Select AmazonSSMFullAccess policy and scroll down.

โ™ฆ Configuring API GATEWAY:

I am using the same Rest API that I used on ML Project-1.

As we canโ€™t directly connect to our lambda function from our local system.
We have to introduce API GATEWAY as their intermediate service, so that over public internet we can connect to the respective API endpoint and invoke the lambda function.

One new resource we are creating as โ€˜/dockerโ€™ that on hit will launch containers.

Select the Lambda that is going to get triggered and then tell SSM to launch docker containers, in my case โ€˜MngDocker.pyโ€™ As we have made some changes, redeploy it.Hit the /docker API endpoint and see the result!check if one docker container is launched.

see, it works really well.

Till now we are just creating containers, letโ€™s do something more.
Now, letโ€™s introduce the Lambda function for the deletion of containers.

Note: Donโ€™t forget to attach policies for accessing SSM.

Create API endpoint, just like shown before and select DockerRM as the Lambda function.

Now for the API to work, use Deploy API again as have made some changes.

Now testing.

Again hit the URL at this time at /rmvcont API endpoint.

AI MODEL CREATED !

We are going to use this AI model multiple times in future.

Developing The Python Code

The main code contains a lot of tasks so itโ€™s better to discuss the subtasks first and then go to the main code. ( concept of Recursion, isnโ€™t it? )

First, letโ€™s talk about how to click a photo using Python code.

Capturing an Image :-

Overall, this code captures a frame from the webcam, displays it in a window named โ€œmy photoโ€, and waits for the Enter key to be pressed. Once the Enter key is pressed, the program exits, and the window is closed.

Letโ€™s break down the code step by step:

  1. `!pip install opencv-python`: This line installs the OpenCV library, specifically the `opencv-python` package, using the `pip` package manager. This package provides the necessary functions and classes for image and video processing tasks.
    From the command prompt, just use โ€œpip install opencv-pythonโ€. Here
    โ€œ!โ€ is commonly used in Jupyter Notebook or JupyterLab environments to execute shell commands directly from within the notebook.

2. `import cv2`: This line imports the OpenCV library, which is used for computer vision tasks like image and video processing.

3. `cap = cv2.VideoCapture(0)`: This line initializes the video capture object `cap` using `cv2.VideoCapture(0)`. It opens the default video capture device (usually the webcam) with an index of 0, indicating the first available camera. `0` means the in-built camera of system.

4. `ret, photo = cap.read()`: This line captures a frame from the video capture device using the `read()` method of the `cap` object. The return value `ret` indicates whether the frame was successfully captured, and `photo` contains the captured frame.
basically `cap.read()` returns two output and for that we used twp variables.

5. `cv2.imshow(โ€œmy photoโ€, photo)`: This line displays the captured frame in a window named โ€œmy photoโ€ using the `imshow()` function from OpenCV. The `photo` variable holds the image data.

6. `if cv2.waitKey() == 13:`: This line waits for a key press event. The `waitKey()` function will block the program until a key is pressed. The value returned by `waitKey()` is then compared with the ASCII value of the Enter key (13).
You can use 27 also, it is the value for `esc` button on keyboard.

7. `cv2.destroyAllWindows()`: This line closes all open windows created by OpenCV. It is called when the Enter key is pressed(in this case as 13 is written here), indicated by the previous condition.

NOTE: Dont forget to release the camera when your work is done.

while loop` is continuously clicking photos every 10ms and showing the picture, providing a smooth video live streaming!!

Hand Detection source code for Program:-

Letโ€™s break down the code step by step:

1. `import cv2`: This line imports the OpenCV library, which is used for computer vision tasks like image and video processing.

2. `from cvzone.HandTrackingModule import HandDetector`: This line imports a specific module called `HandDetector` from the `cvzone` library(dont import all the function but one specific one). This module provides functionality for hand detection and tracking.

3. `detector = HandDetector(maxHands=1, detectionCon=0.8)`: This line creates an instance of the `HandDetector` class with two parameters: `maxHands` and `detectionCon`. `maxHands` is set to 1, which means it will track only one hand. `detectionCon` is set to 0.8, which is the confidence threshold for hand detection. 0.8 means 80% here.

4. `cap = cv2.VideoCapture(0)`: This line initializes the capture object using `cv2.VideoCapture(0)`, which opens the default video capture device (typically the webcam) for capturing video frames.

Capturing a photo, this time with one hand, letโ€™s know if my program can detect my hand!

ret, photo=cap.read()
cv2.imshow("my photo" , photo)
cv2.waitKey()

cv2.destroyAllWindows()

We are using `detector` object to call findHands() function and storing the details in another variable that is `hand`.

`hand = detector.findHands(photo, draw=False)`
The line of code is using a hand detection algorithm to detect and locate hands in an image represented by the โ€œphotoโ€ variable.

The line assigns the result of the detection to the โ€œhandโ€ variable, which likely contains details about the detected hands, such as their positions, landmarks, and other relevant information. The โ€œdraw=Falseโ€ parameter indicates that the detected hands should not be visually annotated or highlighted in the image.

You can use โ€œdraw=Trueโ€ if you want to see all the annotations.

For detecting hands to get progress in project: -

This code is performing some operations on the detected hand stored in the โ€œhandโ€ variable.

The line โ€œd = hand[0]โ€ retrieves the first detected hand from the โ€œhandโ€ variable and assigns it to the variable โ€œdโ€. This line assumes that there is at least one hand detected in the image. We are going to use variable `detecthand` instead of just d for better undestanding.
(As previously, we have set `maxHands=1` while creating `detector` object from the `HandDetector` class).

The next line, โ€œdetector.fingersUp(d)โ€, is calling a function named โ€œfingersUpโ€ of the โ€œdetectorโ€ object, passing the detected hand โ€œdโ€ as an argument. This function is designed to determine which fingers of the hand are raised or extended.

The โ€œfingersUpโ€ function is specifically identifying whether the second (index 1) and third (index 2) fingers of the hand are raised.(As you can see previosuly I have captured a picture where I kept two fingers up that is 2nd and third) The function returns a Boolean value or some form of representation indicating the state of each finger (raised or not raised).
In this case, returning an array of boolean values.

This code is performing some operations on the detected hand stored in the โ€œhandโ€ variable.

The line โ€œd = hand[0]โ€ retrieves the first detected hand from the โ€œhandโ€ variable and assigns it to the variable โ€œdโ€. This line assumes that there is at least one hand detected in the image. We are going to use variable `detecthand` instead of just d for better undestanding.
(As previously, we have set `maxHands=1` while creating `detector` object from the `HandDetector` class).

The next line, โ€œdetector.fingersUp(d)โ€, is calling a function named โ€œfingersUpโ€ of the โ€œdetectorโ€ object, passing the detected hand โ€œdโ€ as an argument. This function is designed to determine which fingers of the hand are raised or extended.

The โ€œfingersUpโ€ function is specifically identifying whether the second (index 1) and third (index 2) fingers of the hand are raised.(As you can see previosuly I have captured a picture where I kept two fingers up that is 2nd and third) The function returns a Boolean value or some form of representation indicating the state of each finger (raised or not raised).
In this case, returning an array of boolean values.

Now coming to the final source code and by integrating the AI model with the finger detection.

Thatโ€™s it from this Blog you can easily made your project successful!

For this Project I would like to specially Thanks My Mentor Mr. Vimal Daga sir for providing us this amount of incredible knowledge and to whole team of Linux World for their support !

Keep Learning, Keep Growing!

--

--

Sonam Thakur
Sonam Thakur

Written by Sonam Thakur

Tech enthusiast | AWS Cloud | Devops Aspirant | Computer Sciences Engineer https://www.linkedin.com/in/sonam-thakur-43a447211/

No responses yet