Spaces:
Runtime error
Runtime error
Commit
·
c6caac4
1
Parent(s):
7701eda
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,102 +16,124 @@ Show your appreciation for this space-age tool by hitting the 'Like' button and
|
|
| 16 |
📧 Contact us: info@foddu.com
|
| 17 |
👍 Like | """
|
| 18 |
|
| 19 |
-
|
| 20 |
-
url = url
|
| 21 |
-
if not os.path.exists(save_name):
|
| 22 |
-
file = requests.get(url)
|
| 23 |
-
open(save_name, 'wb').write(file.content)
|
| 24 |
-
|
| 25 |
-
# Download files
|
| 26 |
-
file_urls = [
|
| 27 |
-
'https://huggingface.co/spaces/foduucom/CandleStickScan-Stock-trading-yolov8/resolve/main/test/-2022-06-28-12-35-50_png.rf.8dee4bb645ea8b5036721b830d2636b1.jpg',
|
| 28 |
-
'https://huggingface.co/spaces/foduucom/CandleStickScan-Stock-trading-yolov8/resolve/main/test/-2022-06-28-12-45-10_png.rf.8b9177546e62a2422ad603b16f1f50b9.jpg',
|
| 29 |
-
'https://www.dropbox.com/s/7sjfwncffg8xej2/video_7.mp4?dl=1'
|
| 30 |
-
]
|
| 31 |
-
|
| 32 |
-
for i, url in enumerate(file_urls):
|
| 33 |
-
if 'mp4' in file_urls[i]:
|
| 34 |
-
download_file(
|
| 35 |
-
file_urls[i],
|
| 36 |
-
f"video.mp4"
|
| 37 |
-
)
|
| 38 |
-
# else:
|
| 39 |
-
# download_file(
|
| 40 |
-
# file_urls[i],
|
| 41 |
-
# f"image_{i}.jpg"
|
| 42 |
-
# )
|
| 43 |
|
| 44 |
# Load YOLO model
|
| 45 |
model = YOLO('foduucom/stockmarket-pattern-detection-yolov8')
|
| 46 |
|
| 47 |
-
|
| 48 |
-
def
|
| 49 |
-
image =
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
| 63 |
inputs_image = [
|
| 64 |
-
gr.
|
| 65 |
-
]
|
| 66 |
-
|
| 67 |
-
gr.
|
|
|
|
|
|
|
| 68 |
]
|
|
|
|
|
|
|
| 69 |
interface_image = gr.Interface(
|
| 70 |
-
fn=
|
| 71 |
inputs=inputs_image,
|
| 72 |
outputs=outputs_image,
|
| 73 |
title=model_heading,
|
| 74 |
-
|
| 75 |
-
examples=
|
| 76 |
cache_examples=False,
|
| 77 |
)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
def show_preds_video(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
cap = cv2.VideoCapture(video_path)
|
| 82 |
-
while(cap.isOpened()):
|
| 83 |
-
ret, frame = cap.read()
|
| 84 |
-
if ret:
|
| 85 |
-
frame_copy = frame.copy()
|
| 86 |
-
outputs = model.predict(source=frame)
|
| 87 |
-
results = outputs[0].cpu().numpy()
|
| 88 |
-
for i, det in enumerate(results.boxes.xyxy):
|
| 89 |
-
cv2.rectangle(
|
| 90 |
-
frame_copy,
|
| 91 |
-
(int(det[0]), int(det[1])),
|
| 92 |
-
(int(det[2]), int(det[3])),
|
| 93 |
-
color=(0, 0, 255),
|
| 94 |
-
thickness=2,
|
| 95 |
-
lineType=cv2.LINE_AA
|
| 96 |
-
)
|
| 97 |
-
yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
inputs_video = [
|
| 100 |
gr.components.Video(type="filepath", label="Input Video"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
]
|
| 103 |
-
outputs_video =
|
| 104 |
-
|
| 105 |
-
]
|
| 106 |
-
|
| 107 |
interface_video = gr.Interface(
|
| 108 |
fn=show_preds_video,
|
| 109 |
inputs=inputs_video,
|
| 110 |
outputs=outputs_video,
|
| 111 |
title=model_heading,
|
| 112 |
-
|
| 113 |
examples=video_path,
|
| 114 |
-
cache_examples=
|
| 115 |
)
|
| 116 |
|
| 117 |
gr.TabbedInterface(
|
|
|
|
| 16 |
📧 Contact us: info@foddu.com
|
| 17 |
👍 Like | """
|
| 18 |
|
| 19 |
+
image_path= [['test/test1.jpg', 'foduucom/stockmarket-pattern-detection-yolov8', 640, 0.25, 0.45], ['test/test2.jpg', 'foduucom/stockmarket-pattern-detection-yolov8', 640, 0.25, 0.45]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Load YOLO model
|
| 22 |
model = YOLO('foduucom/stockmarket-pattern-detection-yolov8')
|
| 23 |
|
| 24 |
+
#############################################################Image Inference############################################################
|
| 25 |
+
def yolov8_img_inference(
|
| 26 |
+
image: gr.inputs.Image = None,
|
| 27 |
+
model_path: gr.inputs.Dropdown = None,
|
| 28 |
+
image_size: gr.inputs.Slider = 640,
|
| 29 |
+
conf_threshold: gr.inputs.Slider = 0.25,
|
| 30 |
+
iou_threshold: gr.inputs.Slider = 0.45,
|
| 31 |
+
):
|
| 32 |
+
"""
|
| 33 |
+
YOLOv8 inference function
|
| 34 |
+
Args:
|
| 35 |
+
image: Input image
|
| 36 |
+
model_path: Path to the model
|
| 37 |
+
image_size: Image size
|
| 38 |
+
conf_threshold: Confidence threshold
|
| 39 |
+
iou_threshold: IOU threshold
|
| 40 |
+
Returns:
|
| 41 |
+
Rendered image
|
| 42 |
+
"""
|
| 43 |
+
model = YOLO(model_path)
|
| 44 |
+
model.overrides['conf'] = conf_threshold
|
| 45 |
+
model.overrides['iou']= iou_threshold
|
| 46 |
+
model.overrides['agnostic_nms'] = False # NMS class-agnostic
|
| 47 |
+
model.overrides['max_det'] = 1000
|
| 48 |
+
image = read_image(image)
|
| 49 |
+
results = model.predict(image)
|
| 50 |
+
render = render_result(model=model, image=image, result=results[0])
|
| 51 |
+
|
| 52 |
+
return render
|
| 53 |
|
| 54 |
+
|
| 55 |
inputs_image = [
|
| 56 |
+
gr.inputs.Image(type="filepath", label="Input Image"),
|
| 57 |
+
gr.inputs.Dropdown(["foduucom/stockmarket-pattern-detection-yolov8"],
|
| 58 |
+
default="foduucom/stockmarket-pattern-detection-yolov8", label="Model"),
|
| 59 |
+
gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
|
| 60 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
|
| 61 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
| 62 |
]
|
| 63 |
+
|
| 64 |
+
outputs_image =gr.outputs.Image(type="filepath", label="Output Image")
|
| 65 |
interface_image = gr.Interface(
|
| 66 |
+
fn=yolov8_img_inference,
|
| 67 |
inputs=inputs_image,
|
| 68 |
outputs=outputs_image,
|
| 69 |
title=model_heading,
|
| 70 |
+
description=description,
|
| 71 |
+
examples=image_path,
|
| 72 |
cache_examples=False,
|
| 73 |
)
|
| 74 |
|
| 75 |
+
##################################################Video Inference################################################################
|
| 76 |
+
def show_preds_video(
|
| 77 |
+
video_path: gr.components.Video = None,
|
| 78 |
+
model_path: gr.inputs.Dropdown = None,
|
| 79 |
+
image_size: gr.inputs.Slider = 640,
|
| 80 |
+
conf_threshold: gr.inputs.Slider = 0.25,
|
| 81 |
+
iou_threshold: gr.inputs.Slider = 0.45,
|
| 82 |
+
):
|
| 83 |
+
"""
|
| 84 |
+
Video inference function
|
| 85 |
+
Args:
|
| 86 |
+
video_path: Input video
|
| 87 |
+
model_path: Path to the model
|
| 88 |
+
image_size: Image size
|
| 89 |
+
conf_threshold: Confidence threshold
|
| 90 |
+
iou_threshold: IOU threshold
|
| 91 |
+
Returns:
|
| 92 |
+
Rendered video
|
| 93 |
+
"""
|
| 94 |
cap = cv2.VideoCapture(video_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
while cap.isOpened():
|
| 97 |
+
success, frame = cap.read()
|
| 98 |
+
|
| 99 |
+
if success:
|
| 100 |
+
model = YOLO(model_path)
|
| 101 |
+
model.overrides['conf'] = conf_threshold
|
| 102 |
+
model.overrides['iou'] = iou_threshold
|
| 103 |
+
model.overrides['agnostic_nms'] = False
|
| 104 |
+
model.overrides['max_det'] = 1000
|
| 105 |
+
results = model.predict(frame)
|
| 106 |
+
annotated_frame = results[0].plot()
|
| 107 |
+
|
| 108 |
+
cv2.imshow("YOLOv8 Inference", annotated_frame)
|
| 109 |
+
|
| 110 |
+
if cv2.waitKey(1) & 0xFF == ord("q"):
|
| 111 |
+
break
|
| 112 |
+
else:
|
| 113 |
+
break
|
| 114 |
+
|
| 115 |
+
cap.release()
|
| 116 |
+
cv2.destroyAllWindows()
|
| 117 |
+
|
| 118 |
inputs_video = [
|
| 119 |
gr.components.Video(type="filepath", label="Input Video"),
|
| 120 |
+
gr.inputs.Dropdown(["foduucom/stockmarket-pattern-detection-yolov8"],
|
| 121 |
+
default="foduucom/stockmarket-pattern-detection-yolov8", label="Model"),
|
| 122 |
+
gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
|
| 123 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
|
| 124 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
| 125 |
|
| 126 |
]
|
| 127 |
+
outputs_video = gr.outputs.Image(type="filepath", label="Output Video")
|
| 128 |
+
video_path=[['test/video.mp4','foduucom/stockmarket-pattern-detection-yolov8', 640, 0.25, 0.45]]
|
|
|
|
|
|
|
| 129 |
interface_video = gr.Interface(
|
| 130 |
fn=show_preds_video,
|
| 131 |
inputs=inputs_video,
|
| 132 |
outputs=outputs_video,
|
| 133 |
title=model_heading,
|
| 134 |
+
description=description,
|
| 135 |
examples=video_path,
|
| 136 |
+
cache_examples=True,
|
| 137 |
)
|
| 138 |
|
| 139 |
gr.TabbedInterface(
|