# Computer Vision Made Easy!

Computer vision fascinates me a lot, these days I am exploring the world of Computer Vision. While working I realised that we have to write hundreds of lines of code just for basic computer vision techniques like face detection, hand detection, pose classification etc.
I explored opencv and mediapipe library which helped me gain a lot of insight into computer vision. I have developed some packages that might help the developer community to focus more on the implementation rather than on coding from scratch. Here are three packages that I developed recently.
### **1. Face Detection**
[Try it out](https://pypi.org/project/lkfacedetection/)

![Screenshot (994).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1663263753498/v7FVuqhFc.png align="left")
Function
=====================

`faceDetector(image, draw=False)`

- **Function Parameter** : This function takes `image` (a single frame) as input and a variable `draw` with default value _False_. You can change the value of parameter `draw` to _True_ if you want to draw the rectangular box over the face on the `image` frame.
- **Output** : This function returns a nested list of length 2. The element at index 1 is the `frame` and a list of `[x,y,w,h]`. `x` is the minimum x co-ordinates of the face, `y` is the minimum y co-ordinate of face, `w` is the width and `h` is the height of the face. NOTE that the `frame` will have _rectangular box over the face if value of `draw` is set to True in the function_.


Usage
=====================

> faceDetector(image, draw=False)

With detection over the face directly through function
____________
```
from lkfacedetection import faceDetector
import cv2
cap = cv2.VideoCapture(0)
while True:
    success, image = cap.read()
    functionValues = faceDetector(image,draw=True) #draw over the frame from function
    frame = functionValues[0]
    cv2.imshow('Face', frame)
    cv2.waitKey(1)
cap.release()
```

With detection externally using the values from function
____________

```
from lkfacedetection import faceDetector
import cv2
cap = cv2.VideoCapture(0)
while True:
    success, image = cap.read()
    functionValues = faceDetector(image) #doesn't draw over the frame
    frame = functionValues[0]
    x,y,w,h = functionValues[1]
    cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) #Draws a rectangle over the face
    cv2.imshow('Face', frame)
    cv2.waitKey(1)
cap.release()
```



### **2. Hand Tracking Package**
[Try it out](https://pypi.org/project/lkhandmapping/)

![Screenshot (995).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1663263862149/wkdarJJIr.png align="left")
Function
=====================

`handTracker(image,draw=True)`

- **Function Parameter** : This function takes `image` (a single frame) as input and a variable `draw` with default value _False_. You can change the value of parameter `draw` to _True_ if you want to the mapping of hands over the `image` frame.
- **Output** : This function returns a nested list of length 2. The element at index 1 is the `frame` and a list of `handLandmarks`. Know more about these `handLandmarks` on this [link](https://google.github.io/mediapipe/solutions/hands.html#hand-landmark-model). NOTE that the _function will return [0] at index 1 in the list if no hands are detected_.


Usage
=====================

> handTracker(image,draw=False)

Mapping over the hands directly from function.
____________
```
from lkhandtracking import handTracker
import cv2

cap = cv2.VideoCapture(0)
while True:
    success, image = cap.read()
    functionValues = handTracker(image,draw=True)
    image = functionValues[0]
    handLms = functionValues[1]
    print(handLms)
    cv2.imshow('Hands', image)
    cv2.waitKey(1)
cap.release()
```

Track hands without mapping.
____________

```
from lkhandtracking import handTracker
import cv2
    
cap = cv2.VideoCapture(0)
while True:
    success, image = cap.read()
    functionValues = handTracker(image,draw=False)
    image = functionValues[0]
    handLms = functionValues[1]
    print(handLms)
    cv2.imshow('Hands', image)
    cv2.waitKey(1)
cap.release()
```


### **3. Body Segmentation**
[Try it out](https://pypi.org/project/lkbodysegmentation/)


![Screenshot (996).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1663263919655/wAzKYrXX2.png align="left")
Function
=====================

`bodySegmentation(orignalImg,backgroundImg=(255,255,255),threshold=0.3)`

- **Function Parameter** : This function takes `orignalImg` i.e. the image on which the segmentation has to happen. Other parameters i.e. `backgroundImg` sets the background color, by default it is white, but it can be changed as per user's need. threshold defines the level at which background has to be removed.
- **Output** : The functions returns a frame as output where the background is segmented.

Usage
=====================

> bodySegmentation(img)

Default Segmentation (White Background)
____________
```
from lkbodysegmentation import bodySegmentation
import cv2

cap = cv2.VideoCapture(0)


while True:
    success, img = cap.read()
    img = bodySegmentation(img)
    cv2.imshow('Image',img)
    cv2.waitKey(1)
```


> bodySegmentation(img, backgroundColor, threshold)

Customized Segmentation
____________
```
from lkbodysegmentation import bodySegmentation
import cv2

cap = cv2.VideoCapture(0)


while True:
    success, img = cap.read()
    backgroundColor = (255,0,255) #You can replace with an image too
    threshold = 0.45 #Level of background to be erased
    img = bodySegmentation(img,backgroundColor,threshold)
    cv2.imshow('Image',img)
    cv2.waitKey(1)
```




Developer
=================
This package is developed by [Lakshay Kumar](https://www.lakshaykumar.tech/) an enthusiastic AI Researcher. This is developed keeping in mind the pain to write lengthy lines of code just to detect faces. This will enable other developers to focus more on implementation part rather than spending time on coding the face detection module.<br>
Feel free to share your feedback via [mail](mailto:contact@lakshaykumar.tech)

