Spaces:
Runtime error
Runtime error
Annas Dev
commited on
Commit
·
f417efa
1
Parent(s):
461480c
add example
Browse files- .gitignore +2 -1
- app.py +32 -4
- detection/app.py +17 -9
.gitignore
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
.DS_Store
|
| 2 |
-
*.pt
|
|
|
|
|
|
| 1 |
.DS_Store
|
| 2 |
+
*.pt
|
| 3 |
+
Inference/
|
app.py
CHANGED
|
@@ -1,16 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from detection import app as detector
|
| 3 |
from recognition import app_easyocr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
def recognize(img_arr):
|
| 6 |
detections = detector.detect(img_arr)
|
| 7 |
return img_arr, detections
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
demo = gr.Interface(fn=recognize,
|
| 11 |
-
inputs=[
|
| 12 |
-
outputs=[
|
|
|
|
| 13 |
description='BIB Race Number Recognition')
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from detection import app as detector
|
| 3 |
from recognition import app_easyocr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import requests
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def recognize(img_arr = None, img_url = None):
|
| 10 |
+
if img_arr == None and img_url != None:
|
| 11 |
+
print('downloading image....')
|
| 12 |
+
img_arr = Image.open(requests.get(img_url, stream=True).raw)
|
| 13 |
+
|
| 14 |
+
if img_arr == None:
|
| 15 |
+
print('choose image or type the url!')
|
| 16 |
+
return
|
| 17 |
|
|
|
|
| 18 |
detections = detector.detect(img_arr)
|
| 19 |
return img_arr, detections
|
| 20 |
|
| 21 |
|
| 22 |
+
input_image = gr.Image(type="pil")
|
| 23 |
+
input_text = gr.Textbox(lines=1, label='Or Image Url', placeholder="paste image url here...")
|
| 24 |
+
output_img = gr.Image(type='pil')
|
| 25 |
+
output_json = gr.JSON()
|
| 26 |
+
|
| 27 |
+
# examples = gr.Examples([[os.path.join(os.path.dirname(__file__), "examples/annas_running.jpg"), 'https://storage.googleapis.com/runpage-uscentral.appspot.com/runpage/79226/eca79d20-b364-11e9-8b83-4794e55a590e.jpg']],
|
| 28 |
+
# inputs=[input_image, input_text],
|
| 29 |
+
# outputs=[output_img, output_json],
|
| 30 |
+
# fn=recognize,
|
| 31 |
+
# cache_examples=True)
|
| 32 |
+
|
| 33 |
demo = gr.Interface(fn=recognize,
|
| 34 |
+
inputs=[input_image, input_text],
|
| 35 |
+
outputs=[output_img, output_json],
|
| 36 |
+
# examples=examples,
|
| 37 |
description='BIB Race Number Recognition')
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch()
|
| 42 |
+
# demo.launch()
|
| 43 |
|
| 44 |
|
detection/app.py
CHANGED
|
@@ -20,14 +20,16 @@ from detection.utils.torch_utils import select_device, load_classifier, time_syn
|
|
| 20 |
from PIL import Image
|
| 21 |
|
| 22 |
#downloading models
|
| 23 |
-
def
|
| 24 |
download_urls = [
|
| 25 |
'https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt',
|
| 26 |
'https://gitlab.com/annasblackhat/bib-weight/-/raw/main/bib-best.pt',
|
| 27 |
'https://gitlab.com/annasblackhat/bib-weight/-/raw/main/bib-best-v2.pt']
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# os.system("wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt")
|
| 33 |
# os.system("wget https://gitlab.com/annasblackhat/bib-weight/-/raw/main/bib-best.pt")
|
|
@@ -39,13 +41,19 @@ except:
|
|
| 39 |
print('dir exist')
|
| 40 |
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
parser = argparse.ArgumentParser()
|
| 48 |
-
parser.add_argument('--weights', nargs='+', type=str, default=
|
| 49 |
parser.add_argument('--source', type=str, default='Inference/', help='source') # file/folder, 0 for webcam
|
| 50 |
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
|
| 51 |
parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
|
|
|
|
| 20 |
from PIL import Image
|
| 21 |
|
| 22 |
#downloading models
|
| 23 |
+
def download_model(url):
|
| 24 |
download_urls = [
|
| 25 |
'https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt',
|
| 26 |
'https://gitlab.com/annasblackhat/bib-weight/-/raw/main/bib-best.pt',
|
| 27 |
'https://gitlab.com/annasblackhat/bib-weight/-/raw/main/bib-best-v2.pt']
|
| 28 |
+
|
| 29 |
+
filename = os.path.basename(url)
|
| 30 |
+
if not os.path.exists(filename):
|
| 31 |
+
os.system("wget " + url)
|
| 32 |
+
return filename
|
| 33 |
|
| 34 |
# os.system("wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt")
|
| 35 |
# os.system("wget https://gitlab.com/annasblackhat/bib-weight/-/raw/main/bib-best.pt")
|
|
|
|
| 41 |
print('dir exist')
|
| 42 |
|
| 43 |
|
| 44 |
+
weight_list = {
|
| 45 |
+
'V1': 'https://gitlab.com/annasblackhat/bib-weight/-/raw/main/bib-best.pt',
|
| 46 |
+
'V2': 'https://gitlab.com/annasblackhat/bib-weight/-/raw/main/bib-best-v2.pt'
|
| 47 |
+
}
|
| 48 |
|
| 49 |
+
#download base model
|
| 50 |
+
download_model('https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt')
|
| 51 |
+
|
| 52 |
+
def detect(img, weight_version = "V2"):
|
| 53 |
+
print('weight: ', weight_version)
|
| 54 |
+
weight_path = download_model(weight_list[weight_version])
|
| 55 |
parser = argparse.ArgumentParser()
|
| 56 |
+
parser.add_argument('--weights', nargs='+', type=str, default=weight_path, help='model.pt path(s)')
|
| 57 |
parser.add_argument('--source', type=str, default='Inference/', help='source') # file/folder, 0 for webcam
|
| 58 |
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
|
| 59 |
parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
|