mintheinwin commited on
Commit
ca5b664
·
1 Parent(s): f3680ca

add update app.py

Browse files
Files changed (2) hide show
  1. .gradio/certificate.pem +31 -0
  2. app.py +129 -27
.gradio/certificate.pem ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
3
+ TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
4
+ cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
5
+ WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
6
+ ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
7
+ MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
8
+ h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
9
+ 0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
10
+ A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
11
+ T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
12
+ B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC
13
+ B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUv
14
+ KBds0pjBqAlk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XRR4Xq37Z0j4r7g1SgEEzwxA57d
30
+ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
31
+ -----END CERTIFICATE-----
app.py CHANGED
@@ -1,43 +1,145 @@
1
- from ultralytics import YOLO
2
- from PIL import Image
3
  import gradio as gr
4
  from huggingface_hub import snapshot_download
 
5
  import os
 
 
 
6
 
7
- #public model path location
8
  #MODEL_REPO_ID = "mintheinwin/3907578Y"
9
 
10
- #Organizations model path location
11
  MODEL_REPO_ID = "ITI107-2024S2/3907578Y"
12
 
13
- #load model
14
  def load_model(repo_id):
15
  download_dir = snapshot_download(repo_id)
16
- print(download_dir)
17
- path = os.path.join(download_dir, "best_int8_openvino_model")
18
- print(path)
19
- detection_model = YOLO(path, task='detect')
20
  return detection_model
21
-
 
22
  detection_model = load_model(MODEL_REPO_ID)
23
 
24
- #Student ID
25
  student_info = "Student Id: 3907578Y, Name: Min Thein Win"
26
 
27
- #prdeict
28
- def predict(pilimg):
29
- source = pilimg
30
- result = detection_model.predict(source, conf=0.5, iou=0.5)
31
- img_bgr = result[0].plot()
32
- out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
33
  return out_pilimg
34
-
35
- #UI interface
36
- gr.Markdown("# Wild Animal Detection (Tiger/Lion)")
37
- gr.Markdown(student_info)
38
- gr.Interface(fn=predict,
39
- inputs=gr.Image(type="pil",label="Input"),
40
- outputs=gr.Image(type="pil",label="Output"),
41
- title="Wild Animal Detection (Tiger/Lion)",
42
- description=student_info,
43
- ).launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import snapshot_download
3
+ from ultralytics import YOLO
4
  import os
5
+ from PIL import Image
6
+ import cv2
7
+ import tempfile
8
 
9
+ #Public model path location
10
  #MODEL_REPO_ID = "mintheinwin/3907578Y"
11
 
12
+ #Organization's(NYP) model path location
13
  MODEL_REPO_ID = "ITI107-2024S2/3907578Y"
14
 
15
+ # Load model
16
  def load_model(repo_id):
17
  download_dir = snapshot_download(repo_id)
18
+ path = os.path.join(download_dir, "best_int8_openvino_model")
19
+ detection_model = YOLO(path, task="detect")
 
 
20
  return detection_model
21
+
22
+ # Initialize the model
23
  detection_model = load_model(MODEL_REPO_ID)
24
 
25
+ # Student info
26
  student_info = "Student Id: 3907578Y, Name: Min Thein Win"
27
 
28
+ # Prediction function for images
29
+ def predict_image(pil_img):
30
+ result = detection_model.predict(pil_img, conf=0.5, iou=0.5)
31
+ img_bgr = result[0].plot() # Annotated image
32
+ out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # Convert to RGB PIL image
 
33
  return out_pilimg
34
+
35
+ # Prediction function for videos
36
+ def predict_video(video):
37
+ cap = cv2.VideoCapture(video)
38
+ temp_dir = tempfile.mkdtemp()
39
+ output_path = os.path.join(temp_dir, "annotated_video.mp4")
40
+
41
+ frames = []
42
+ while cap.isOpened():
43
+ ret, frame = cap.read()
44
+ if not ret:
45
+ break
46
+
47
+ # Perform detection on each frame
48
+ result = detection_model.predict(frame, conf=0.5, iou=0.5)
49
+ annotated_frame = result[0].plot()
50
+ frames.append(annotated_frame)
51
+
52
+ cap.release()
53
+
54
+ # Save annotated video
55
+ if frames:
56
+ height, width, _ = frames[0].shape
57
+ out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), 20, (width, height))
58
+ for frame in frames:
59
+ out.write(frame)
60
+ out.release()
61
+
62
+ return output_path
63
+
64
+ # UI Interface
65
+ def create_interface():
66
+ with gr.Blocks() as interface:
67
+ # Header
68
+ gr.Markdown("# Wild Animal Detection (Lion/Tiger)")
69
+ gr.Markdown(student_info)
70
+
71
+ with gr.Row(equal_height=True): # Input and output view are aligned vertically
72
+ # Input Section
73
+ with gr.Column(scale=1):
74
+ gr.Markdown("### Detection Input Image/Video:")
75
+ input_type = gr.Radio(
76
+ choices=["Image", "Video"],
77
+ value="Image", # Default selection is "Image"
78
+ label="Select Input Type",
79
+ interactive=True,
80
+ )
81
+ input_image = gr.Image(type="pil", label="Upload Image", visible=True) # Default is visible
82
+ input_video = gr.Video(label="Upload Video", visible=False) # Default is hidden
83
+
84
+ def toggle_inputs(input_choice):
85
+ if input_choice == "Image":
86
+ return gr.update(visible=True), gr.update(visible=False)
87
+ else:
88
+ return gr.update(visible=False), gr.update(visible=True)
89
+
90
+ input_type.change(toggle_inputs, inputs=input_type, outputs=[input_image, input_video])
91
+
92
+ # Output Section
93
+ with gr.Column(scale=1): # Adjust scale for proportional width
94
+ gr.Markdown("### Detection Output Image/Video:")
95
+ gr.Markdown(
96
+ "<p style=color: gray;'>"
97
+ "This section displays detected objects with bounding boxes, animal types (Lion/Tiger), and confidence levels.</p>")
98
+ output_image = gr.Image(type="pil", label="Detection Result (Image)", visible=True) # Default is visible
99
+ output_video = gr.Video(label="Detection Result (Video)", visible=False) # Default is hidden
100
+
101
+ #Detect Function
102
+ def detect(input_choice, image=None, video=None):
103
+ if input_choice == "Image" and image is not None:
104
+ return predict_image(image), None
105
+ elif input_choice == "Video" and video is not None:
106
+ return None, predict_video(video)
107
+ return None, None
108
+
109
+ # Buttons in Horizontal Layout
110
+ with gr.Row():
111
+ detect_btn = gr.Button("START DETECT", variant="primary")
112
+ reset_btn = gr.Button("RESET", variant="secondary")
113
+
114
+ detect_btn.click(
115
+ fn=detect,
116
+ inputs=[input_type, input_image, input_video],
117
+ outputs=[output_image, output_video],
118
+ )
119
+
120
+ # Reset Function
121
+ def reset_view():
122
+ return None, None
123
+
124
+ reset_btn.click(fn=reset_view, inputs=None, outputs=[output_image, output_video])
125
+
126
+ # Dynamic Output Visibility
127
+ def update_output_visibility(input_choice):
128
+ if input_choice == "Image":
129
+ return gr.update(visible=True), gr.update(visible=False)
130
+ else:
131
+ return gr.update(visible=False), gr.update(visible=True)
132
+
133
+ input_type.change(update_output_visibility, inputs=input_type, outputs=[output_image, output_video])
134
+
135
+ #Footer view
136
+ gr.Markdown("---")
137
+ gr.Markdown(
138
+ "<p style='text-align: center;color: gray;'>"
139
+ "Developed by NYP student @ Min Thein Win</p>"
140
+ )
141
+ return interface
142
+
143
+ # Launch App
144
+ interface = create_interface()
145
+ interface.launch(share=True)