Spaces:
Runtime error
Runtime error
nehulagrawal
commited on
Commit
β’
9424877
1
Parent(s):
d3ad207
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import cv2
|
4 |
+
import requests
|
5 |
+
import os
|
6 |
+
|
7 |
+
from ultralyticsplus import YOLO, render_result
|
8 |
+
|
9 |
+
# Model Heading and Description
|
10 |
+
model_heading = "StockMarket: Trends Recognition for Trading Success"
|
11 |
+
description = """ π―οΈ Light up your trading game with Trend prediction! We decode Trends mysteries like trading Sherlock! π΅οΈββοΈ From 'Down' to 'Up' Trends, we've got patterns covered. Powered by Foduu AI's magic, we'll be your trading Gandalf. Whether you're a trading guru or just starting, we've got your back. πΌπ°
|
12 |
+
π Trading with Trends is like having a secret trading sauce. Curious? Reach out at [email protected] and unveil the magic! Liking us won't give you superpowers, but it's a step towards trading wizardry! πππ―οΈ
|
13 |
+
π§ Contact us: [email protected]
|
14 |
+
π Like | Join the Trading Adventure!"""
|
15 |
+
|
16 |
+
image_path= [['test/test1.jpg', 'foduucom/stockmarket-future-prediction', 640, 0.25, 0.45], ['test/test2.jpg', 'foduucom/foduucom/stockmarket-future-prediction', 640, 0.25, 0.45],['test/test3.jpg', 'foduucom/stockmarket-future-prediction', 640, 0.25, 0.45]]
|
17 |
+
|
18 |
+
# Load YOLO model
|
19 |
+
model = YOLO('foduucom/stockmarket-future-prediction')
|
20 |
+
|
21 |
+
#############################################################Image Inference############################################################
|
22 |
+
def yolov8_img_inference(
|
23 |
+
image: gr.inputs.Image = None,
|
24 |
+
model_path: gr.inputs.Dropdown = None,
|
25 |
+
image_size: gr.inputs.Slider = 640,
|
26 |
+
conf_threshold: gr.inputs.Slider = 0.25,
|
27 |
+
iou_threshold: gr.inputs.Slider = 0.45,
|
28 |
+
):
|
29 |
+
"""
|
30 |
+
YOLOv8 inference function
|
31 |
+
Args:
|
32 |
+
image: Input image
|
33 |
+
model_path: Path to the model
|
34 |
+
image_size: Image size
|
35 |
+
conf_threshold: Confidence threshold
|
36 |
+
iou_threshold: IOU threshold
|
37 |
+
Returns:
|
38 |
+
Rendered image
|
39 |
+
"""
|
40 |
+
model = YOLO(model_path)
|
41 |
+
model.overrides['conf'] = conf_threshold
|
42 |
+
model.overrides['iou']= iou_threshold
|
43 |
+
model.overrides['agnostic_nms'] = False # NMS class-agnostic
|
44 |
+
model.overrides['max_det'] = 1000
|
45 |
+
# image = read_image(image)
|
46 |
+
results = model.predict(image)
|
47 |
+
render = render_result(model=model, image=image, result=results[0])
|
48 |
+
|
49 |
+
return render
|
50 |
+
|
51 |
+
|
52 |
+
inputs_image = [
|
53 |
+
gr.inputs.Image(type="filepath", label="Input Image"),
|
54 |
+
gr.inputs.Dropdown(["foduucom/stockmarket-future-prediction"],
|
55 |
+
default="foduucom/stockmarket-future-prediction", label="Model"),
|
56 |
+
gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
|
57 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
|
58 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
59 |
+
]
|
60 |
+
|
61 |
+
outputs_image =gr.outputs.Image(type="filepath", label="Output Image")
|
62 |
+
interface_image = gr.Interface(
|
63 |
+
fn=yolov8_img_inference,
|
64 |
+
inputs=inputs_image,
|
65 |
+
outputs=outputs_image,
|
66 |
+
title=model_heading,
|
67 |
+
description=description,
|
68 |
+
examples=image_path,
|
69 |
+
cache_examples=False,
|
70 |
+
theme='huggingface'
|
71 |
+
)
|
72 |
+
|
73 |
+
##################################################Video Inference################################################################
|
74 |
+
def show_preds_video(
|
75 |
+
video_path: str = None,
|
76 |
+
model_path: str = None,
|
77 |
+
image_size: int = 640,
|
78 |
+
conf_threshold: float = 0.25,
|
79 |
+
iou_threshold: float = 0.45,
|
80 |
+
):
|
81 |
+
cap = cv2.VideoCapture(video_path)
|
82 |
+
|
83 |
+
while cap.isOpened():
|
84 |
+
success, frame = cap.read()
|
85 |
+
|
86 |
+
if success:
|
87 |
+
model = YOLO(model_path)
|
88 |
+
model.overrides['conf'] = conf_threshold
|
89 |
+
model.overrides['iou'] = iou_threshold
|
90 |
+
model.overrides['agnostic_nms'] = False
|
91 |
+
model.overrides['max_det'] = 1000
|
92 |
+
results = model.predict(frame)
|
93 |
+
annotated_frame = results[0].plot()
|
94 |
+
|
95 |
+
# Do not display the frame using cv2.imshow
|
96 |
+
# cv2.imshow("YOLOv8 Inference", annotated_frame)
|
97 |
+
|
98 |
+
# Break the loop if 'q' is pressed
|
99 |
+
if cv2.waitKey(1) & 0xFF == ord("q"):
|
100 |
+
break
|
101 |
+
else:
|
102 |
+
break
|
103 |
+
|
104 |
+
cap.release()
|
105 |
+
cv2.destroyAllWindows()
|
106 |
+
|
107 |
+
|
108 |
+
inputs_video = [
|
109 |
+
gr.components.Video(type="filepath", label="Input Video"),
|
110 |
+
gr.inputs.Dropdown(["foduucom/stockmarket-future-prediction"],
|
111 |
+
default="foduucom/stockmarket-future-prediction", label="Model"),
|
112 |
+
gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
|
113 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
|
114 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
115 |
+
|
116 |
+
]
|
117 |
+
outputs_video = gr.outputs.Image(type="filepath", label="Output Video")
|
118 |
+
video_path=[['test/testvideo.mp4','foduucom/stockmarket-future-prediction', 640, 0.25, 0.45]]
|
119 |
+
interface_video = gr.Interface(
|
120 |
+
fn=show_preds_video,
|
121 |
+
inputs=inputs_video,
|
122 |
+
outputs=outputs_video,
|
123 |
+
title=model_heading,
|
124 |
+
description=description,
|
125 |
+
examples=video_path,
|
126 |
+
cache_examples=False,
|
127 |
+
theme='huggingface'
|
128 |
+
)
|
129 |
+
|
130 |
+
gr.TabbedInterface(
|
131 |
+
[interface_image, interface_video],
|
132 |
+
tab_names=['Image inference', 'Video inference']
|
133 |
+
).queue().launch()
|
134 |
+
|