Skip to content

Commit 9d3b985

Browse files
committed
Add Python-AI-ML
1 parent 2d9a696 commit 9d3b985

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+24516
-1
lines changed

.DS_Store

2 KB
Binary file not shown.

media-files/.DS_Store

6 KB
Binary file not shown.

media-files/api.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask import Flask, request
2+
from datetime import datetime
23

34
app = Flask(__name__)
45
app.secret_key = 'Superrandom'
@@ -14,7 +15,7 @@ def file():
1415

1516

1617
print(a)
17-
a.save('./store/'+a.filename)
18+
a.save('./data/'+str(datetime.now())+"-" + a.filename)
1819

1920
# f = open("test.jpg", 'wb')
2021
# f.write(a.read())

media-files/data/.DS_Store

6 KB
Binary file not shown.
160 KB
Binary file not shown.
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

media-files/tim.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from datetime import datetime
2+
3+
while True:
4+
d = datetime.now()
5+
if (d.second == 2 or d.second == 34 ) and d.microsecond == 0:
6+
print(d.time())
7+

python-ai-ml-master/.gitignore

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
.vscode
2+
imageai/model.h5
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[cod]
6+
*$py.class
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
build/
14+
develop-eggs/
15+
dist/
16+
downloads/
17+
eggs/
18+
.eggs/
19+
lib/
20+
lib64/
21+
parts/
22+
sdist/
23+
var/
24+
wheels/
25+
pip-wheel-metadata/
26+
share/python-wheels/
27+
*.egg-info/
28+
.installed.cfg
29+
*.egg
30+
MANIFEST
31+
32+
# PyInstaller
33+
# Usually these files are written by a python script from a template
34+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
35+
*.manifest
36+
*.spec
37+
38+
# Installer logs
39+
pip-log.txt
40+
pip-delete-this-directory.txt
41+
42+
# Unit test / coverage reports
43+
htmlcov/
44+
.tox/
45+
.nox/
46+
.coverage
47+
.coverage.*
48+
.cache
49+
nosetests.xml
50+
coverage.xml
51+
*.cover
52+
*.py,cover
53+
.hypothesis/
54+
.pytest_cache/
55+
56+
# Translations
57+
*.mo
58+
*.pot
59+
60+
# Django stuff:
61+
*.log
62+
local_settings.py
63+
db.sqlite3
64+
db.sqlite3-journal
65+
66+
# Flask stuff:
67+
instance/
68+
.webassets-cache
69+
70+
# Scrapy stuff:
71+
.scrapy
72+
73+
# Sphinx documentation
74+
docs/_build/
75+
76+
# PyBuilder
77+
target/
78+
79+
# Jupyter Notebook
80+
.ipynb_checkpoints
81+
82+
# IPython
83+
profile_default/
84+
ipython_config.py
85+
86+
# pyenv
87+
.python-version
88+
89+
# pipenv
90+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
91+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
92+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
93+
# install all needed dependencies.
94+
#Pipfile.lock
95+
96+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
97+
__pypackages__/
98+
99+
# Celery stuff
100+
celerybeat-schedule
101+
celerybeat.pid
102+
103+
# SageMath parsed files
104+
*.sage.py
105+
106+
# Environments
107+
.env
108+
.venv
109+
env/
110+
venv/
111+
ENV/
112+
env.bak/
113+
venv.bak/
114+
115+
# Spyder project settings
116+
.spyderproject
117+
.spyproject
118+
119+
# Rope project settings
120+
.ropeproject
121+
122+
# mkdocs documentation
123+
/site
124+
125+
# mypy
126+
.mypy_cache/
127+
.dmypy.json
128+
dmypy.json
129+
130+
# Pyre type checker
131+
.pyre/

python-ai-ml-master/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# python-ai-ml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
8+
[packages]
9+
10+
[requires]
11+
python_version = "3.8"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Gesture Recognition
2+
3+
Recognizing "Hand Gestures" using OpenCV and Python.
4+
5+
#### Libraries needed
6+
7+
* cv2
8+
* imutils
9+
* numpy
10+
* sklearn
11+
12+
#### Usage
13+
14+
1. `python segment.py`
15+
2. `python recognize.py`
16+
3. `python recognize-image.py`
17+
18+
To understand how this works, please read the blog posts given below.
19+
20+
* [Hand Gesture Recognition using Python and OpenCV - Part 1](https://gogul09.github.io/software/hand-gesture-recognition-p1)
21+
* [Hand Gesture Recognition using Python and OpenCV - Part 2](https://gogul09.github.io/software/hand-gesture-recognition-p2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Gogul
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#------------------------------------------------------------
2+
# SEGMENT, RECOGNIZE and COUNT fingers from a single frame
3+
#------------------------------------------------------------
4+
5+
# organize imports
6+
import cv2
7+
import imutils
8+
import numpy as np
9+
from sklearn.metrics import pairwise
10+
11+
#---------------------------------------------
12+
# To segment the region of hand in the image
13+
#---------------------------------------------
14+
def segment(image, grayimage, threshold=75):
15+
# threshold the image to get the foreground which is the hand
16+
thresholded = cv2.threshold(grayimage, threshold, 255, cv2.THRESH_BINARY)[1]
17+
18+
# get the contours in the thresholded image
19+
(_, cnts, _) = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
20+
21+
# return None, if no contours detected
22+
if len(cnts) == 0:
23+
return
24+
else:
25+
# based on contour area, get the maximum contour which is the hand
26+
segmented = max(cnts, key=cv2.contourArea)
27+
28+
return (thresholded, segmented)
29+
30+
#--------------------------------------------------------------
31+
# To count the number of fingers in the segmented hand region
32+
#--------------------------------------------------------------
33+
def count(image, thresholded, segmented):
34+
# find the convex hull of the segmented hand region
35+
# which is the maximum contour with respect to area
36+
chull = cv2.convexHull(segmented)
37+
38+
# find the most extreme points in the convex hull
39+
extreme_top = tuple(chull[chull[:, :, 1].argmin()][0])
40+
extreme_bottom = tuple(chull[chull[:, :, 1].argmax()][0])
41+
extreme_left = tuple(chull[chull[:, :, 0].argmin()][0])
42+
extreme_right = tuple(chull[chull[:, :, 0].argmax()][0])
43+
44+
# find the center of the palm
45+
cX = int((extreme_left[0] + extreme_right[0]) / 2)
46+
cY = int((extreme_top[1] + extreme_bottom[1]) / 2)
47+
48+
# find the maximum euclidean distance between the center of the palm
49+
# and the most extreme points of the convex hull
50+
distances = pairwise.euclidean_distances([(cX, cY)], Y=[extreme_left, extreme_right, extreme_top, extreme_bottom])[0]
51+
max_distance = distances[distances.argmax()]
52+
53+
# calculate the radius of the circle with 80% of the max euclidean distance obtained
54+
radius = int(0.8 * max_distance)
55+
56+
# find the circumference of the circle
57+
circumference = (2 * np.pi * radius)
58+
59+
# initialize circular_roi with same shape as thresholded image
60+
circular_roi = np.zeros(thresholded.shape[:2], dtype="uint8")
61+
62+
# draw the circular ROI with radius and center point of convex hull calculated above
63+
cv2.circle(circular_roi, (cX, cY), radius, 255, 1)
64+
65+
# take bit-wise AND between thresholded hand using the circular ROI as the mask
66+
# which gives the cuts obtained using mask on the thresholded hand image
67+
circular_roi = cv2.bitwise_and(thresholded, thresholded, mask=circular_roi)
68+
69+
# compute the contours in the circular ROI
70+
(_, cnts, _) = cv2.findContours(circular_roi.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
71+
72+
count = 0
73+
# approach 1 - eliminating wrist
74+
#cntsSorted = sorted(cnts, key=lambda x: cv2.contourArea(x))
75+
#print(len(cntsSorted[1:])) # gives the count of fingers
76+
77+
# approach 2 - eliminating wrist
78+
# loop through the contours found
79+
for i, c in enumerate(cnts):
80+
81+
# compute the bounding box of the contour
82+
(x, y, w, h) = cv2.boundingRect(c)
83+
84+
# increment the count of fingers only if -
85+
# 1. The contour region is not the wrist (bottom area)
86+
# 2. The number of points along the contour does not exceed
87+
# 25% of the circumference of the circular ROI
88+
if ((cY + (cY * 0.25)) > (y + h)) and ((circumference * 0.25) > c.shape[0]):
89+
count += 1
90+
91+
return count
92+
93+
#-----------------
94+
# MAIN FUNCTION
95+
#-----------------
96+
if __name__ == "__main__":
97+
# get the current frame
98+
frame = cv2.imread("resources/hand-sample.jpg")
99+
100+
# resize the frame
101+
frame = imutils.resize(frame, width=700)
102+
103+
# clone the frame
104+
clone = frame.copy()
105+
106+
# get the height and width of the frame
107+
(height, width) = frame.shape[:2]
108+
109+
# convert the frame to grayscale and blur it
110+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
111+
gray = cv2.GaussianBlur(gray, (7, 7), 0)
112+
113+
# segment the hand region
114+
hand = segment(clone, gray)
115+
116+
# check whether hand region is segmented
117+
if hand is not None:
118+
# if yes, unpack the thresholded image and segmented contour
119+
(thresholded, segmented) = hand
120+
121+
# count the number of fingers
122+
fingers = count(clone, thresholded, segmented)
123+
124+
cv2.putText(clone, "This is " + str(fingers), (70, 45), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
125+
126+
# display the frame with segmented hand
127+
cv2.imshow("Image", clone)
128+
129+
cv2.waitKey(0)
130+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)