Spaces:
Build error
Build error
shashankanand13monu
commited on
Commit
·
36c998e
1
Parent(s):
4bbb1e2
Added App
Browse files- Screenshot 2022-01-09 161732.png +0 -0
- demo.jpg +0 -0
- hand-fist-making-communism-symbol-260nw-286581497.jpg +0 -0
- istockphoto-1179377734-612x612 (1).jpg +0 -0
- istockphoto-1179377734-612x612 (3).jpg +0 -0
- istockphoto-1179377734-612x612 (4).jpg +0 -0
- istockphoto-1179377734-612x612.jpg +0 -0
- output.mp4 +0 -0
- requirements.txt +7 -0
- st2.py +549 -0
- tempCodeRunnerFile.py +2 -0
- test3.mp4 +0 -0
- woman-showing-four-fingers-white-background-woman-showing-four-fingers-white-background-closeup-hand-134504006.jpg +0 -0
- wsad.jpg +0 -0
Screenshot 2022-01-09 161732.png
ADDED
![]() |
demo.jpg
ADDED
![]() |
hand-fist-making-communism-symbol-260nw-286581497.jpg
ADDED
![]() |
istockphoto-1179377734-612x612 (1).jpg
ADDED
![]() |
istockphoto-1179377734-612x612 (3).jpg
ADDED
![]() |
istockphoto-1179377734-612x612 (4).jpg
ADDED
![]() |
istockphoto-1179377734-612x612.jpg
ADDED
![]() |
output.mp4
ADDED
Binary file (44 Bytes). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.3.1
|
2 |
+
numpy
|
3 |
+
pandas
|
4 |
+
opencv-python-headless
|
5 |
+
Pillow==8.3.2
|
6 |
+
temp
|
7 |
+
mediapipe==0.8.7
|
st2.py
ADDED
@@ -0,0 +1,549 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
+
page_title="Game Keys",
|
5 |
+
page_icon="🎮",
|
6 |
+
layout="wide",
|
7 |
+
initial_sidebar_state="expanded"
|
8 |
+
)
|
9 |
+
import pandas as pd
|
10 |
+
import numpy as np
|
11 |
+
import cv2
|
12 |
+
import mediapipe as mp
|
13 |
+
import time
|
14 |
+
from PIL import Image
|
15 |
+
import tempfile
|
16 |
+
|
17 |
+
DEMO_IMAGE = 'demo.jpg'
|
18 |
+
DEMO_VIDEO = 'test3.mp4'
|
19 |
+
prevTime=0
|
20 |
+
currTime=0
|
21 |
+
tipIds= [4,8,12,16,20]
|
22 |
+
st.title('Game Keys with Hand Tracking Web-App')
|
23 |
+
import webbrowser
|
24 |
+
|
25 |
+
url = 'https://github.com/shashankanand13monu/Game-Automation'
|
26 |
+
|
27 |
+
# if st.button('Code'):
|
28 |
+
# webbrowser.open_new_tab(url)
|
29 |
+
|
30 |
+
# st.set_page_config(
|
31 |
+
# page_title="Ex-stream-ly Cool App",
|
32 |
+
# page_icon="🧊",
|
33 |
+
# layout="wide",
|
34 |
+
# initial_sidebar_state="expanded"
|
35 |
+
# )
|
36 |
+
|
37 |
+
mp_drawing = mp.solutions.drawing_utils
|
38 |
+
mp_draw= mp.solutions.drawing_utils
|
39 |
+
mp_face_mesh = mp.solutions.face_mesh
|
40 |
+
mp_draw= mp.solutions.drawing_utils
|
41 |
+
mp_hand= mp.solutions.hands
|
42 |
+
mp_hands = mp.solutions.hands
|
43 |
+
# mp_drawing = mp.solutions.drawing_utils
|
44 |
+
mp_drawing_styles = mp.solutions.drawing_styles
|
45 |
+
# mp_hands = mp.solutions.hands
|
46 |
+
# mp_hand= mp.solutions.hands
|
47 |
+
# mp_drawing = mp.solutions.drawing_utils
|
48 |
+
# mp_drawing_styles = mp.solutions.drawing_styles
|
49 |
+
# mp_hands = mp.solutions.hands
|
50 |
+
t = st.empty()
|
51 |
+
def draw(str):
|
52 |
+
# t.subheader(str)
|
53 |
+
# original_title = 'f<p style="font-family:Arial Black; color:red; font-size: 30px;">{str}</p>'
|
54 |
+
# st.markdown('f<p style="font-family:Arial Black; color:red; font-size: 30px;">{str}</p>', unsafe_allow_html=True)
|
55 |
+
t.markdown(f'<p style="font-family:Arial Black;color:#FF0686;font-size:28px;;">{str}</p>', unsafe_allow_html=True)
|
56 |
+
# variable_output = str
|
57 |
+
# font_size = 30
|
58 |
+
|
59 |
+
# html_str = f"""
|
60 |
+
# <style>
|
61 |
+
# p.a {{
|
62 |
+
# font: bold {font_size}px Courier;
|
63 |
+
# }}
|
64 |
+
# </style>
|
65 |
+
# <p class="a">{variable_output}</p>
|
66 |
+
# """
|
67 |
+
|
68 |
+
# st.markdown(html_str, unsafe_allow_html=True)
|
69 |
+
|
70 |
+
st.markdown(
|
71 |
+
"""
|
72 |
+
<style>
|
73 |
+
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child{
|
74 |
+
width: 350px
|
75 |
+
}
|
76 |
+
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child{
|
77 |
+
width: 350px
|
78 |
+
margin-left: -350px
|
79 |
+
</style>
|
80 |
+
""",unsafe_allow_html=True,)
|
81 |
+
if st.sidebar.button('Code'):
|
82 |
+
webbrowser.open_new_tab(url)
|
83 |
+
st.sidebar.title('Menu')
|
84 |
+
|
85 |
+
st.sidebar.subheader('Settings')
|
86 |
+
|
87 |
+
@st.cache ()
|
88 |
+
def image_resize(image, width=None, height=None, inter =cv2.INTER_AREA):
|
89 |
+
|
90 |
+
dim = None
|
91 |
+
(h ,w) = image.shape[:2]
|
92 |
+
if width is None and height is None:
|
93 |
+
return image
|
94 |
+
if width is None:
|
95 |
+
r= width/float(w)
|
96 |
+
dim = (int(w*r), height)
|
97 |
+
else:
|
98 |
+
r = width/float(w)
|
99 |
+
dim = (width, int(h*r))
|
100 |
+
#resize the image
|
101 |
+
resized =cv2.resize(image, dim ,interpolation=inter)
|
102 |
+
return resized
|
103 |
+
|
104 |
+
app_mode= st.sidebar.selectbox('Choose the App Mode',
|
105 |
+
['About App','Run on Image','Run On Video'])
|
106 |
+
|
107 |
+
# st.sidebar.markdown('---')
|
108 |
+
# st.sidebar.button('Code')
|
109 |
+
# st.sibutton('Code')
|
110 |
+
# if st.sidebar.button('Code'):
|
111 |
+
# webbrowser.open_new_tab(url)
|
112 |
+
|
113 |
+
if app_mode== 'About App':
|
114 |
+
st.markdown('App Made using **Mediapipe** & **Open CV**')
|
115 |
+
|
116 |
+
st.markdown(
|
117 |
+
"""
|
118 |
+
<style>
|
119 |
+
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child{
|
120 |
+
width: 350px
|
121 |
+
}
|
122 |
+
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child{
|
123 |
+
width: 350px
|
124 |
+
margin-left: -350px
|
125 |
+
</style>
|
126 |
+
""",unsafe_allow_html=True,)
|
127 |
+
|
128 |
+
st.markdown('''
|
129 |
+
# Tutorial \n
|
130 |
+
|
131 |
+
'''
|
132 |
+
)
|
133 |
+
original_title = '<pre style="font-family:Aku & Kamu; color:#FD0177; font-size: 25px;font-weight:Bold">🕹️ W- 5 Fingers 🕹️ A- 2 or 3 Fingers</pre>'
|
134 |
+
st.markdown(original_title, unsafe_allow_html=True)
|
135 |
+
original_title = '<pre style="font-family:Aku & Kamu; color:#FD0177; font-size: 25px;font-weight:Bold;">🕹️ S- Fist 🕹️ D- 4 Fingers</pre>'
|
136 |
+
st.markdown(original_title, unsafe_allow_html=True)
|
137 |
+
# st.subheader('''W - 5 Fingers''')
|
138 |
+
# st.subheader('S - Fist\n A - 2 or 3 Fingers\n D - 4 Fingers')
|
139 |
+
st.image('wsad.jpg',width=200)
|
140 |
+
original_title = '<pre style="font-family:Aku & Kamu; color:#FD0101 ; font-size: 28px;font-weight:Bold">*NOTE</pre>'
|
141 |
+
st.markdown(original_title, unsafe_allow_html=True)
|
142 |
+
original_title= '''<pre style="font-family:Aku & Kamu; color:#270F40; font-size: 24px;">
|
143 |
+
Video Option will Experience Lag in Browsers.
|
144 |
+
If It's <strong>Lagging</strong> just <strong>Reload</strong> & Choose your option <strong>ASAP</strong>
|
145 |
+
eg: <strong>Choosing Max Hands</strong> or <strong>Using Webcam.</strong>
|
146 |
+
Webcam Will Take about <strong>20 Seconds</strong> to Load</pre>'''
|
147 |
+
# st.markdown('''Video Option will Experience **Lag** in **Browsers**. If It's **Lagging** just **Reload** & Choose your option ASAP eg: **Choosing Max Hands** or **Using Webcam**. Webcam Will Take about **20 Seconds** to Load ''')
|
148 |
+
st.markdown(original_title, unsafe_allow_html=True)
|
149 |
+
|
150 |
+
|
151 |
+
elif app_mode == 'Run on Image':
|
152 |
+
drawing_spec = mp_drawing.DrawingSpec(thickness=2, circle_radius=1)
|
153 |
+
st.sidebar.markdown ('---------' )
|
154 |
+
|
155 |
+
st.markdown(
|
156 |
+
"""
|
157 |
+
<style>
|
158 |
+
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child{
|
159 |
+
width: 350px
|
160 |
+
}
|
161 |
+
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child{
|
162 |
+
width: 350px
|
163 |
+
margin-left: -350px
|
164 |
+
</style>
|
165 |
+
""",unsafe_allow_html=True,)
|
166 |
+
|
167 |
+
# st.markdown("**Detected Hands**")
|
168 |
+
st.header("** Detected Hands **")
|
169 |
+
kpi1_text = st.markdown("0")
|
170 |
+
|
171 |
+
max_hands= st.sidebar.number_input('Maximum Number of Hands',value=2,min_value=1,max_value=4)
|
172 |
+
# st.sidebar('---')
|
173 |
+
detection_confidence= st.sidebar.slider('Detection Confidence',min_value=0.0,max_value=1.0,value=0.5)
|
174 |
+
st.sidebar.markdown('---')
|
175 |
+
IMAGE_FILE=[]
|
176 |
+
count=0
|
177 |
+
|
178 |
+
img_file_buffer = st.sidebar.file_uploader("Upload an Image", type=["jpg","jpeg", "png"])
|
179 |
+
if img_file_buffer is not None:
|
180 |
+
file_bytes = np.asarray(bytearray(img_file_buffer.read()), dtype=np.uint8)
|
181 |
+
opencv_image = cv2.imdecode(file_bytes, 1)
|
182 |
+
image = opencv_image.copy()
|
183 |
+
# image = np.array(Image.open(img_file_buffer))
|
184 |
+
else:
|
185 |
+
demo_image= DEMO_IMAGE
|
186 |
+
# file_bytes = np.asarray(bytearray(demo_image.read()), dtype=np.uint8)
|
187 |
+
# opencv_image = cv2.imdecode(file_bytes, 1)
|
188 |
+
# image = opencv_image.copy()
|
189 |
+
# image= np.array(Image.open(demo_image))
|
190 |
+
image = 'demo.jpg'
|
191 |
+
cap = cv2.imread('demo.jpg', cv2.IMREAD_UNCHANGED)
|
192 |
+
image = cap.copy()
|
193 |
+
|
194 |
+
# st.sidebar.text('Input Image')
|
195 |
+
st.sidebar.subheader('Input Image')
|
196 |
+
image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
|
197 |
+
st.sidebar.image(image)
|
198 |
+
# st.sidebar.markdown('---')
|
199 |
+
# st.sidebar.text('Demo Image')
|
200 |
+
st.sidebar.subheader('Demo Images')
|
201 |
+
# st.sidebar.image('WIN_20211106_14_34_25_Pro.jpg')
|
202 |
+
st.sidebar.image('360_F_61499604_hkfPSZ4ZYs47Yp8H780DEb3I3cvWjdmH.jpg')
|
203 |
+
st.sidebar.image('Screenshot 2022-01-09 161732.png')
|
204 |
+
st.sidebar.image('woman-showing-four-fingers-white-background-woman-showing-four-fingers-white-background-closeup-hand-134504006.jpg')
|
205 |
+
|
206 |
+
st.sidebar.image('demo.jpg')
|
207 |
+
hand_count =0
|
208 |
+
image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR)
|
209 |
+
cap = cv2.imread('demo.jpg', cv2.IMREAD_UNCHANGED)
|
210 |
+
# xxx= image
|
211 |
+
# xxx= f'{image}'
|
212 |
+
# IMAGE_FILE.append(xxx)
|
213 |
+
# plt.imshow(image_batch[i].numpy().astype("uint8"))
|
214 |
+
# IMAGE_FILE.append(image)
|
215 |
+
# image = cv2.imread(opencv_image, cv2.IMREAD_UNCHANGED)
|
216 |
+
# IMAGE_FILES.append(image)
|
217 |
+
with mp_hands.Hands(
|
218 |
+
static_image_mode=True,
|
219 |
+
max_num_hands=max_hands,
|
220 |
+
min_detection_confidence=detection_confidence) as hands:
|
221 |
+
|
222 |
+
hand_count+=1
|
223 |
+
# for idx, file in enumerate(IMAGE_FILES):
|
224 |
+
# Read an image, flip it around y-axis for correct handedness output (see
|
225 |
+
# above).
|
226 |
+
# image = cv2.flip(cv2.imread(file), 1)
|
227 |
+
# Convert the BGR image to RGB before processing.
|
228 |
+
results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
229 |
+
|
230 |
+
# Print handedness and draw hand landmarks on the image.
|
231 |
+
# int('Handedness:', results.multi_handedness)
|
232 |
+
# if not results.multi_hand_landmarks:
|
233 |
+
# continue
|
234 |
+
try:
|
235 |
+
|
236 |
+
age_height, image_width, _ = image.shape
|
237 |
+
annotated_image = image.copy()
|
238 |
+
lmList=[]
|
239 |
+
lmList2forModel=[]
|
240 |
+
for hand_landmarks in results.multi_hand_landmarks:
|
241 |
+
# print('hand_landmarks:', hand_landmarks)
|
242 |
+
# print(
|
243 |
+
# f'Index finger tip coordinates: (',
|
244 |
+
# f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * image_width}, '
|
245 |
+
# f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * image_height})'
|
246 |
+
# )
|
247 |
+
myHands=results.multi_hand_landmarks[0]
|
248 |
+
|
249 |
+
for id,lm in enumerate(myHands.landmark):
|
250 |
+
|
251 |
+
h,w,c=image.shape
|
252 |
+
cx,cy=int(lm.x*w), int(lm.y*h)
|
253 |
+
lmList.append([id,cx,cy])
|
254 |
+
lmList2forModel.append([cx,cy])
|
255 |
+
fingers=[]
|
256 |
+
if lmList[tipIds[0]][1] < lmList[tipIds[0]-1][1]:
|
257 |
+
fingers.append(1)
|
258 |
+
|
259 |
+
else:
|
260 |
+
fingers.append(0)
|
261 |
+
|
262 |
+
|
263 |
+
for id in range(1,5):
|
264 |
+
if lmList[tipIds[id]][2] < lmList[tipIds[id]-1][2]:
|
265 |
+
fingers.append(1)
|
266 |
+
|
267 |
+
|
268 |
+
else:
|
269 |
+
fingers.append(0)
|
270 |
+
|
271 |
+
total= fingers.count(1)
|
272 |
+
|
273 |
+
if total==5:
|
274 |
+
# st.text('Acclerate')
|
275 |
+
# st.subheader("Acclerate")
|
276 |
+
# mmm=st.markdown("Acclerate")
|
277 |
+
# st.markdown('---')
|
278 |
+
original_title = '<p style="font-family:Arial Black; color:#FD0177; font-size: 30px;">Acclerate</p>'
|
279 |
+
st.markdown(original_title, unsafe_allow_html=True)
|
280 |
+
# st.markdown('---')
|
281 |
+
|
282 |
+
|
283 |
+
overlay = cv2.imread('istockphoto-1179377734-612x612.jpg')
|
284 |
+
overlay = cv2.resize(overlay,(100,100))
|
285 |
+
x_offset = 80
|
286 |
+
y_offset = 10
|
287 |
+
x_end = x_offset + overlay.shape[1]
|
288 |
+
y_end = y_offset + overlay.shape[0]
|
289 |
+
annotated_image[y_offset:y_end,x_offset:x_end] = overlay
|
290 |
+
|
291 |
+
if total==4:
|
292 |
+
# st.text('Right')
|
293 |
+
original_title = '<p style="font-family:Arial Black; color:#FD0177; font-size: 30px;">Right</p>'
|
294 |
+
st.markdown(original_title, unsafe_allow_html=True)
|
295 |
+
overlay = cv2.imread('istockphoto-1179377734-612x612 (4).jpg')
|
296 |
+
overlay = cv2.resize(overlay,(100,100))
|
297 |
+
x_offset = 120
|
298 |
+
y_offset = 50
|
299 |
+
x_end = x_offset + overlay.shape[1]
|
300 |
+
y_end = y_offset + overlay.shape[0]
|
301 |
+
annotated_image[y_offset:y_end,x_offset:x_end] = overlay
|
302 |
+
|
303 |
+
if total==2 or total==3:
|
304 |
+
# st.text('Left')
|
305 |
+
original_title = '<p style="font-family:Arial Black; color:#FD0177; font-size: 30px;">Left</p>'
|
306 |
+
st.markdown(original_title, unsafe_allow_html=True)
|
307 |
+
overlay = cv2.imread('istockphoto-1179377734-612x612 (1).jpg')
|
308 |
+
overlay = cv2.resize(overlay,(100,100))
|
309 |
+
x_offset = 50
|
310 |
+
y_offset = 50
|
311 |
+
x_end = x_offset + overlay.shape[1]
|
312 |
+
y_end = y_offset + overlay.shape[0]
|
313 |
+
annotated_image[y_offset:y_end,x_offset:x_end] = overlay
|
314 |
+
|
315 |
+
if total==0:
|
316 |
+
# st.text('Brake')
|
317 |
+
original_title = '<p style="font-family:Arial Black; color:#FD0177; font-size: 30px;">Brake</p>'
|
318 |
+
st.markdown(original_title, unsafe_allow_html=True)
|
319 |
+
overlay = cv2.imread('istockphoto-1179377734-612x612 (3).jpg')
|
320 |
+
overlay = cv2.resize(overlay,(100,100))
|
321 |
+
x_offset = 50
|
322 |
+
y_offset = 90
|
323 |
+
x_end = x_offset + overlay.shape[1]
|
324 |
+
y_end = y_offset + overlay.shape[0]
|
325 |
+
annotated_image[y_offset:y_end,x_offset:x_end] = overlay
|
326 |
+
mp_drawing.draw_landmarks(
|
327 |
+
annotated_image,
|
328 |
+
hand_landmarks,
|
329 |
+
mp_hands.HAND_CONNECTIONS,
|
330 |
+
mp_draw.DrawingSpec(color=(0,0,255), thickness=2, circle_radius=2),
|
331 |
+
mp_draw.DrawingSpec(color=(0,255,0), thickness=2, circle_radius=2))
|
332 |
+
# annotated_image= cv2.cvtColor(annotated_image,cv2.COLOR_BGR2RGB)
|
333 |
+
kpi1_text.write(f"<h1 style='text-align: center; color:red; '>{hand_count}</h1>", unsafe_allow_html=True)
|
334 |
+
# st.subheader('Output Image')
|
335 |
+
# st.image(annotated_image, use_column_width=True)
|
336 |
+
except TypeError:
|
337 |
+
# st.text('Sorry No Hand Found!!')
|
338 |
+
original_title = '<p style="font-family:Arial Black; color:Red; font-size: 30px;">Sorry, No Hand Found!!</p>'
|
339 |
+
st.markdown(original_title, unsafe_allow_html=True)
|
340 |
+
|
341 |
+
kpi1_text.write(f"<h1 style='text-align: center; color:red; '>{0}</h1>", unsafe_allow_html=True)
|
342 |
+
|
343 |
+
# annotated_image= cv2.cvtColor(annotated_image,cv2.COLOR_BGR2RGB)
|
344 |
+
# kpi1_text.write(f"<h1 style='text-align: center; color:red; '>{hand_count}</h1>", unsafe_allow_html=True)
|
345 |
+
st.subheader('Output Image')
|
346 |
+
annotated_image = cv2.cvtColor(annotated_image,cv2.COLOR_BGR2RGB)
|
347 |
+
st.image(annotated_image, use_column_width=False)
|
348 |
+
|
349 |
+
elif app_mode == 'Run On Video':
|
350 |
+
|
351 |
+
st.set_option('deprecation.showfileUploaderEncoding',False)
|
352 |
+
# st.text('This is some text.')
|
353 |
+
# st.text('This is somext.')
|
354 |
+
use_webcam = st.sidebar.button('Use Webcam')
|
355 |
+
record= st.sidebar.checkbox("Record Video")
|
356 |
+
|
357 |
+
if record:
|
358 |
+
st.checkbox("Recording",value=True)
|
359 |
+
|
360 |
+
# drawing_spec = mp_drawing.DrawingSpec(thickness=2, circle_radius=1)
|
361 |
+
# st.sidebar.markdown ('---------' )
|
362 |
+
|
363 |
+
st.markdown(
|
364 |
+
"""
|
365 |
+
<style>
|
366 |
+
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child{
|
367 |
+
width: 350px
|
368 |
+
}
|
369 |
+
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child{
|
370 |
+
width: 350px
|
371 |
+
margin-left: -350px
|
372 |
+
</style>
|
373 |
+
""",unsafe_allow_html=True,)
|
374 |
+
|
375 |
+
# st.markdown("**Detected Hands**")
|
376 |
+
# kpi1_text = st.markdown("0")
|
377 |
+
|
378 |
+
max_hands= st.sidebar.number_input('Maximum Number of Hand',value=1,min_value=1,max_value=4)
|
379 |
+
# st.sidebar('---')
|
380 |
+
detection_confidence= st.sidebar.slider('Detection Confidence',min_value=0.0,max_value=1.0,value=0.5)
|
381 |
+
tracking_confidence= st.sidebar.slider('Tracking Confidence Confidence',min_value=0.0,max_value=1.0,value=0.5)
|
382 |
+
st.sidebar.markdown('---')
|
383 |
+
|
384 |
+
# st.markdown("Output")
|
385 |
+
st.subheader("Input Video")
|
386 |
+
|
387 |
+
stframe = st.empty()
|
388 |
+
video_file_buffer = st.sidebar.file_uploader("Upload a Video", type=['mp4', 'mov', 'avi', 'asf', 'm4v'])
|
389 |
+
tffile = tempfile.NamedTemporaryFile(delete=False)
|
390 |
+
##We get our input video here
|
391 |
+
if not video_file_buffer:
|
392 |
+
if use_webcam:
|
393 |
+
vid = cv2.VideoCapture(0)
|
394 |
+
else:
|
395 |
+
vid = cv2.VideoCapture(DEMO_VIDEO)
|
396 |
+
tffile.name = DEMO_VIDEO
|
397 |
+
else:
|
398 |
+
tffile.write(video_file_buffer.read())
|
399 |
+
vid = cv2.VideoCapture(tffile.name)
|
400 |
+
width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
|
401 |
+
height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
402 |
+
fps_input = int(vid.get(cv2.CAP_PROP_FPS))
|
403 |
+
#Recording Part
|
404 |
+
codec = cv2.VideoWriter_fourcc('V', 'P', '0','9')
|
405 |
+
out= cv2.VideoWriter('output.mp4',codec,fps_input,(width,height))
|
406 |
+
|
407 |
+
st.sidebar.text('Input Video')
|
408 |
+
st.sidebar.video(tffile.name)
|
409 |
+
# st.markdown('Reload , if webpage hangs')
|
410 |
+
|
411 |
+
fps = 0
|
412 |
+
i = 0
|
413 |
+
drawing_spec = mp_drawing.DrawingSpec(thickness=2, circle_radius=1)
|
414 |
+
kpi1, kpi2, kpi3 = st.columns(3)
|
415 |
+
with kpi1:
|
416 |
+
# st.markdown("**Frame Rate**")
|
417 |
+
original_title = '<p style="text-align: center; font-size: 20px;"><strong>Frame Rate</strong></p>'
|
418 |
+
st.markdown(original_title, unsafe_allow_html=True)
|
419 |
+
kpi1_text = st.markdown ("0")
|
420 |
+
with kpi2:
|
421 |
+
# st.markdown("**Detected Hands**")
|
422 |
+
original_title = '<p style="text-align: center; font-size: 20px;"><strong>Detected Hands</strong></p>'
|
423 |
+
st.markdown(original_title, unsafe_allow_html=True)
|
424 |
+
kpi2_text = st.markdown ("0")
|
425 |
+
with kpi3:
|
426 |
+
# st.markdown("**Width**")
|
427 |
+
original_title = '<p style="text-align: center; font-size: 20px;"><strong>Video Width</strong></p>'
|
428 |
+
st.markdown(original_title, unsafe_allow_html=True)
|
429 |
+
kpi3_text = st.markdown("0")
|
430 |
+
st.markdown ("<hr/>", unsafe_allow_html=True)
|
431 |
+
st.subheader('Reload , if webpage hangs')
|
432 |
+
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
|
433 |
+
# st.subheader("Input Video")
|
434 |
+
|
435 |
+
with mp_hand.Hands(max_num_hands=max_hands,min_detection_confidence=detection_confidence,
|
436 |
+
min_tracking_confidence=tracking_confidence) as hands:
|
437 |
+
|
438 |
+
|
439 |
+
while vid.isOpened():
|
440 |
+
|
441 |
+
i +=1
|
442 |
+
ret, image = vid.read()
|
443 |
+
# image.set(3, 640)
|
444 |
+
# image.set(4, 480)
|
445 |
+
if not ret:
|
446 |
+
continue
|
447 |
+
|
448 |
+
|
449 |
+
image.flags.writeable=False
|
450 |
+
results= hands.process(image)
|
451 |
+
image.flags.writeable=True
|
452 |
+
image= cv2.cvtColor(image,cv2.COLOR_RGB2BGR)
|
453 |
+
|
454 |
+
lmList=[]
|
455 |
+
lmList2forModel=[]
|
456 |
+
hand_count=0
|
457 |
+
if results.multi_hand_landmarks:
|
458 |
+
|
459 |
+
# original_title = 'Acclerate'
|
460 |
+
|
461 |
+
|
462 |
+
# Face Landmark Drawing
|
463 |
+
for hand_landmark in results.multi_hand_landmarks:
|
464 |
+
hand_count += 1
|
465 |
+
myHands=results.multi_hand_landmarks[0]
|
466 |
+
for id,lm in enumerate(myHands.landmark):
|
467 |
+
h,w,c=image.shape
|
468 |
+
cx,cy=int(lm.x*w), int(lm.y*h)
|
469 |
+
lmList.append([id,cx,cy])
|
470 |
+
lmList2forModel.append([cx,cy])
|
471 |
+
|
472 |
+
if lmList[tipIds[0]][1] > lmList[tipIds[0]-1][1]:
|
473 |
+
fingers.append(1)
|
474 |
+
|
475 |
+
else:
|
476 |
+
fingers.append(0)
|
477 |
+
|
478 |
+
|
479 |
+
for id in range(1,5):
|
480 |
+
if lmList[tipIds[id]][2] < lmList[tipIds[id]-1][2]:
|
481 |
+
fingers.append(1)
|
482 |
+
|
483 |
+
|
484 |
+
else:
|
485 |
+
fingers.append(0)
|
486 |
+
|
487 |
+
total= fingers.count(1)
|
488 |
+
# print('**********')
|
489 |
+
# print(total)
|
490 |
+
if total==5:
|
491 |
+
# print('brake')
|
492 |
+
# st.text('Acclerate')
|
493 |
+
# original_title= 'Acc'
|
494 |
+
sh= "Acclerate"
|
495 |
+
draw(sh)
|
496 |
+
if total==2 or total==3:
|
497 |
+
sh= "Left"
|
498 |
+
draw(sh)
|
499 |
+
if total==4:
|
500 |
+
sh= "Right"
|
501 |
+
draw(sh)
|
502 |
+
if total==0:
|
503 |
+
sh= "Brake"
|
504 |
+
draw(sh)
|
505 |
+
# time.sleep(1)
|
506 |
+
# original_title = '<p style="font-family:Arial Black; color:Blue; font-size: 30px;">{original_title}</p>'
|
507 |
+
# st.markdown(original_title, unsafe_allow_html=True)
|
508 |
+
|
509 |
+
mp_draw.draw_landmarks(image,hand_landmark,mp_hand.HAND_CONNECTIONS,
|
510 |
+
mp_draw.DrawingSpec(color=(0,0,255), thickness=2, circle_radius=2),
|
511 |
+
mp_draw.DrawingSpec(color=(0,255,0), thickness=2, circle_radius=2))
|
512 |
+
#FPS Counter Logic
|
513 |
+
currTime = time.time()
|
514 |
+
fps = 1/ (currTime - prevTime)
|
515 |
+
prevTime = currTime
|
516 |
+
fingers=[]
|
517 |
+
|
518 |
+
if record:
|
519 |
+
out.write(image)
|
520 |
+
image= cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
|
521 |
+
kpi1_text.write(f"<h1 style='text-align: center; color:red; '>{int(fps)}</h1>", unsafe_allow_html=True)
|
522 |
+
kpi2_text.write(f"<h1 style='text-align: center; color:red; '>{hand_count}</h1>", unsafe_allow_html=True)
|
523 |
+
|
524 |
+
kpi3_text.write(f"<h1 style='text-align: center; color:red; '>{width}</h1>", unsafe_allow_html=True)
|
525 |
+
|
526 |
+
image = cv2.resize(image, (0,0), fx = 0.8, fy =0.8)
|
527 |
+
image = image_resize(image = image, width = 320,height=360)
|
528 |
+
stframe.image(image, channels = 'BGR', use_column_width=False)
|
529 |
+
# else:
|
530 |
+
# output_video = open('output1.mp4','rb')
|
531 |
+
# out_bytes= output_video.read()
|
532 |
+
# st.video(out_bytes)
|
533 |
+
st.subheader('Output Image')
|
534 |
+
st.text('Video Processed')
|
535 |
+
output_video = open('output1.mp4','rb')
|
536 |
+
out_bytes= output_video.read()
|
537 |
+
st.video(out_bytes)
|
538 |
+
# st.markdown('Reload , if webpage hangs')
|
539 |
+
|
540 |
+
vid.release()
|
541 |
+
out.release()
|
542 |
+
|
543 |
+
|
544 |
+
|
545 |
+
|
546 |
+
|
547 |
+
|
548 |
+
|
549 |
+
|
tempCodeRunnerFile.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# fingers.appen
|
test3.mp4
ADDED
Binary file (5.54 MB). View file
|
|
woman-showing-four-fingers-white-background-woman-showing-four-fingers-white-background-closeup-hand-134504006.jpg
ADDED
![]() |
wsad.jpg
ADDED
![]() |