msn-enginenova21 commited on
Commit
28bfb66
·
1 Parent(s): a7ae08c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import cv2
4
+ import numpy as np
5
+ import subprocess
6
+ import shutil
7
+ import os
8
+ import torch
9
+ import time
10
+ import streamlit_analytics
11
+
12
+
13
+ def clear_detect_directory():
14
+ detect_directory = "yolov5/runs/detect"
15
+ if os.path.exists(detect_directory):
16
+ shutil.rmtree(detect_directory)
17
+ os.makedirs(detect_directory)
18
+
19
+
20
+ def save_image():
21
+ st.title("Hand Sign Detection")
22
+ col1, col2 = st.columns(2) # 2 for two col
23
+ pd_df = None
24
+ with col1:
25
+ genre = st.radio(
26
+ "Upload Your Hand Sign",
27
+ ('Browse', 'Camera'))
28
+
29
+ if genre == 'Camera':
30
+ uploaded_image = st.camera_input("Take a picture")
31
+
32
+ else:
33
+ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
34
+
35
+ if uploaded_image is not None:
36
+
37
+ # Convert the image to a format compatible with PIL and OpenCV
38
+ pil_image = Image.open(uploaded_image)
39
+ opencv_image = np.array(pil_image)
40
+ opencv_image = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2RGB)
41
+
42
+ # Provide a file path to save the image
43
+ upload_image_path = "processed_image.jpg"
44
+ cv2.imwrite(upload_image_path, opencv_image)
45
+
46
+ st.success(f"Image saved as {upload_image_path}")
47
+ st.success("Processing Image...")
48
+
49
+ # label of image:
50
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt') # local model
51
+ results = model(upload_image_path)
52
+ pd_df = (results.pandas().xyxy[0])
53
+
54
+ # max_confidence_name = pd_df.loc[pd_df['confidence'].idxmax(), 'name']
55
+
56
+ clear_detect_directory()
57
+
58
+ command = [
59
+ "python",
60
+ "yolov5/detect.py",
61
+ "--weights", "best.pt",
62
+ "--img", "416",
63
+ "--conf", "0.50",
64
+ "--source", upload_image_path
65
+ ]
66
+
67
+ process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
68
+ std_out, std_err = process.communicate()
69
+ if process.returncode != 0:
70
+ error_message = f"Error: {std_err}"
71
+ st.text(error_message)
72
+
73
+ with col2:
74
+ detect_image_pred = "yolov5/runs/detect/exp/processed_image.jpg"
75
+ if os.path.exists(detect_image_pred):
76
+ st.text("Detected Gesture")
77
+ st.image(detect_image_pred, caption="Detected Image", use_column_width=True)
78
+
79
+ st.text("Detection class probabilities")
80
+ if pd_df is not None:
81
+ st.text(pd_df)
82
+ else:
83
+ pass
84
+ else:
85
+ st.text("Detection Threshold is 60")
86
+ st.text("Detection Gesture")
87
+ st.text("Note: Make clean Gesture if not detected try another Gesture")
88
+ st.image("Untitled_img.png")
89
+
90
+
91
+
92
+ streamlit_analytics.start_tracking()
93
+
94
+ save_image()
95
+
96
+ streamlit_analytics.stop_tracking()
97
+