nakamura196 commited on
Commit
43eb482
1 Parent(s): 2417fbd
Files changed (2) hide show
  1. app.py +34 -51
  2. model_- 19 may 2024 15_13.pt → best.pt +2 -2
app.py CHANGED
@@ -1,65 +1,48 @@
1
  import gradio as gr
2
- import json
 
 
3
 
4
- from ultralyticsplus import YOLO, render_result
5
 
6
- # Model Heading and Description
7
- model_heading = "YOLOv8 NDL-DocL Datasets"
8
- description = """YOLOv8 NDL-DocL Datasets Gradio demo for object detection. Upload an image or click an example image to use."""
9
 
10
- article = "<p style='text-align: center'>YOLOv5 NDL-DocL Datasets is an object detection model trained on the <a href=\"https://github.com/ndl-lab/layout-dataset\">NDL-DocL Datasets</a>.</p>"
 
 
11
 
12
- image_path= [
13
-
14
- ['『源氏物語』(東京大学総合図書館所蔵).jpg', 0.25, 0.45],
15
- ['『源氏物語』(京都大学所蔵).jpg', 0.25, 0.45],
16
- ['『平家物語』(国文学研究資料館提供).jpg', 0.25, 0.45]
17
- ]
18
 
19
- # Load YOLO model
20
- model = YOLO('model_- 19 may 2024 15_13.pt')
21
 
22
- def yolov8_img_inference(
23
- image: gr.Image = None,
24
- conf_threshold: gr.Slider = 0.25,
25
- iou_threshold: gr.Slider = 0.45,
26
- ):
27
- """
28
- YOLOv8 inference function
29
- Args:
30
- image: Input image
31
- conf_threshold: Confidence threshold
32
- iou_threshold: IOU threshold
33
- Returns:
34
- Rendered image
35
- """
36
- results = model.predict(image, conf=conf_threshold, iou=iou_threshold)
37
- render = render_result(model=model, image=image, result=results[0])
38
 
39
- json_data = json.loads(results[0].tojson())
40
-
41
- return render, json_data
42
 
43
-
44
- inputs_image = [
45
- gr.Image(type="filepath", label="Input Image"),
46
- gr.Slider(minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"),
47
- gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
48
  ]
49
 
50
- outputs_image =[
51
- gr.Image(type="filepath", label="Output Image"),
52
- gr.JSON(label="Output JSON")
 
 
 
 
 
 
 
53
  ]
54
- demo = gr.Interface(
55
- fn=yolov8_img_inference,
56
- inputs=inputs_image,
57
- outputs=outputs_image,
58
- title=model_heading,
59
- description=description,
60
- examples=image_path,
61
- article=article,
62
- cache_examples=False
63
- )
64
 
65
  demo.launch(share=False)
 
1
  import gradio as gr
2
+ import torch
3
+ from PIL import Image
4
+ import numpy as np
5
 
6
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
7
 
8
+ import json
 
 
9
 
10
+ def yolo(im, size=1024):
11
+ g = (size / max(im.size)) # gain
12
+ im = im.resize((int(x * g) for x in im.size), Image.LANCZOS) # resize
13
 
14
+ results = model(im) # inference
15
+ if results.imgs:
16
+ writable_img = np.copy(results.imgs[0]) # Create a writable copy of the image data
17
+ results.imgs[0] = writable_img
18
+ results.render() # updates results.imgs with boxes and labels
 
19
 
20
+ df = results.pandas().xyxy[0].to_json(orient="records")
21
+ res = json.loads(df)
22
 
23
+ return [
24
+ Image.fromarray(results.imgs[0]),
25
+ res
26
+ ]
 
 
 
 
 
 
 
 
 
 
 
 
27
 
 
 
 
28
 
29
+ inputs = gr.Image(type='pil', label="Original Image")
30
+ outputs = [
31
+ gr.Image(type="pil", label="Output Image"),
32
+ gr.JSON()
 
33
  ]
34
 
35
+ title = "YOLOv5 NDL-DocL Datasets"
36
+ description = "YOLOv5 NDL-DocL Datasets Gradio demo for object detection. Upload an image or click an example image to use."
37
+ article = "<p style='text-align: center'>YOLOv5 NDL-DocL Datasets is an object detection model trained on the <a href=\"https://github.com/ndl-lab/layout-dataset\">NDL-DocL Datasets</a>.</p>"
38
+
39
+ # <a href=\"aaa\"></a>
40
+
41
+ examples = [
42
+ ['『源氏物語』(東京大学総合図書館所蔵).jpg'],
43
+ ['『源氏物語』(京都大学所蔵).jpg'],
44
+ ['『平家物語』(国文学研究資料館提供).jpg']
45
  ]
46
+ demo = gr.Interface(yolo, inputs, outputs, title=title, description=description, article=article, examples=examples)
 
 
 
 
 
 
 
 
 
47
 
48
  demo.launch(share=False)
model_- 19 may 2024 15_13.pt → best.pt RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:8acb10e585c68b31142f2f4470606da52ce054ab467368b6bfe22ca547620e12
3
- size 136737193
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:497dd54a738a54abfc938507c16b77e4e46930372e6e189ae87895aa5bba0b7c
3
+ size 173348189