File size: 2,923 Bytes
28bfb66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fde305
 
 
28bfb66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d520648
 
28bfb66
 
 
 
 
 
 
 
 
90608bb
28bfb66
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import streamlit as st
from PIL import Image
import cv2
import numpy as np
import subprocess
import shutil
import os
import torch
import time
import streamlit_analytics


def clear_detect_directory():
    detect_directory = "yolov5/runs/detect"
    if os.path.exists(detect_directory):
        shutil.rmtree(detect_directory)
    os.makedirs(detect_directory)


def save_image():
    st.title("Hand Sign Detection")
    col1, col2 = st.columns(2)  # 2 for two col
    pd_df = None
    with col1:
        genre = st.radio(
            "Upload Your Hand Sign",
            ('Browse', 'Camera'))

        if genre == 'Camera':
            uploaded_image = st.camera_input("Take a picture")

        else:
            uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])

        if uploaded_image is not None:

            # Convert the image to a format compatible with PIL and OpenCV
            pil_image = Image.open(uploaded_image)
            opencv_image = np.array(pil_image)
            opencv_image = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2RGB)

            # Provide a file path to save the image
            upload_image_path = "processed_image.jpg"
            cv2.imwrite(upload_image_path, opencv_image)

            st.text("Detection class probabilities")
            st.success(f"Processing Image as {upload_image_path}")


            # label of image:
            model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')  # local model
            results = model(upload_image_path)
            pd_df = (results.pandas().xyxy[0])

            # max_confidence_name = pd_df.loc[pd_df['confidence'].idxmax(), 'name']

            clear_detect_directory()

            command = [
                "python",
                "yolov5/detect.py",
                "--weights", "best.pt",
                "--img", "416",
                "--conf", "0.50",
                "--source", upload_image_path
            ]

            process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
            std_out, std_err = process.communicate()
            if process.returncode != 0:
                error_message = f"Error: {std_err}"
                st.text(error_message)

            st.text(pd_df)

    with col2:
        detect_image_pred = "yolov5/runs/detect/exp/processed_image.jpg"
        if os.path.exists(detect_image_pred):
            st.text("Detected Gesture")
            st.image(detect_image_pred, caption="Detected Image", use_column_width=True)

        else:
            st.text("Detection Threshold is 60")
    st.text("Detection Gesture")
    st.text("Note: Detection model is train on limited Gestures (as shown in example image) Make clean Gesture if not detected try another Gesture")
    st.image("Untitled_img.png")



streamlit_analytics.start_tracking()

save_image()

streamlit_analytics.stop_tracking()