nakamura196 commited on
Commit
85dfa0d
1 Parent(s): 0d2f5ff

feat: initial commit

Browse files
.gitignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ .DS_Store
2
+ yolo*.pt
3
+ # __pycache__
4
+ gradio_queue.db
5
+ __pycache__
6
+ .venv
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Yolov11x Codh Char
3
  emoji: 👁
4
  colorFrom: pink
5
  colorTo: green
 
1
  ---
2
+ title: Yolov11x Char
3
  emoji: 👁
4
  colorFrom: pink
5
  colorTo: green
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ from PIL import Image, ImageDraw
4
+ from ultralytics import YOLO
5
+
6
+ # Model Heading and Description
7
+ model_heading = "YOLOv11x Character"
8
+ description = """YOLOv11x Character Gradio demo for object detection. Upload an image or click an example image to use."""
9
+
10
+ article = "<p style='text-align: center'>YOLOv11x Character is an object detection model trained on the <a href=\"http://codh.rois.ac.jp/char-shape/\">日本古典籍くずし字データセット</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('best.pt')
21
+
22
+
23
+ def get_color(score):
24
+ """Returns color based on confidence score."""
25
+ if score > 0.75:
26
+ return "blue" # 高スコアに濃い青
27
+ elif score > 0.5:
28
+ return "deepskyblue" # 中スコアに明るい青
29
+ elif score > 0.25:
30
+ return "lightblue" # 低スコアに薄い青
31
+ else:
32
+ return "gray" # 非常に低いスコアにグレー
33
+
34
+ def draw_boxes(image_path, results):
35
+ # Open image
36
+ image = Image.open(image_path)
37
+ draw = ImageDraw.Draw(image)
38
+
39
+ # 画像の短辺に基づいて矩形の線の太さを調整
40
+ min_dimension = min(image.size) # 画像の短辺を取得
41
+ line_width = max(1, min_dimension // 200) # 線の太さを短辺の1%程度に設定(最小値は1)
42
+
43
+ # Draw boxes
44
+ for item in results:
45
+ box = item['box']
46
+ # label = item['class']
47
+ score = item['confidence']
48
+
49
+ # Define box coordinates
50
+ x1, y1, x2, y2 = box["x1"], box["y1"], box["x2"], box["y2"]
51
+
52
+ color = get_color(score)
53
+
54
+ # Draw rectangle and label
55
+ draw.rectangle([x1, y1, x2, y2], outline=color, width=line_width)
56
+ # draw.text((x1, y1), f"{label} {score:.2f}", fill=color)
57
+
58
+ return image
59
+
60
+ def YOLOv11x_img_inference(
61
+ image: gr.Image = None,
62
+ conf_threshold: gr.Slider = 0.25,
63
+ iou_threshold: gr.Slider = 0.45,
64
+ ):
65
+ """
66
+ YOLOv11x inference function
67
+ Args:
68
+ image: Input image
69
+ conf_threshold: Confidence threshold
70
+ iou_threshold: IOU threshold
71
+ Returns:
72
+ Rendered image
73
+ JSON output
74
+ """
75
+ results = model.predict(image, conf=conf_threshold, iou=iou_threshold, device="cpu")
76
+
77
+ json_data = json.loads(results[0].tojson())
78
+
79
+ # Draw boxes on image
80
+ result_image = draw_boxes(image, json_data)
81
+
82
+ return result_image, json_data
83
+
84
+
85
+ inputs_image = [
86
+ gr.Image(type="filepath", label="Input Image"),
87
+ gr.Slider(minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"),
88
+ gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
89
+ ]
90
+
91
+ outputs_image =[
92
+ gr.Image(type="filepath", label="Output Image"),
93
+ gr.JSON(label="Output JSON")
94
+ ]
95
+ demo = gr.Interface(
96
+ fn=YOLOv11x_img_inference,
97
+ inputs=inputs_image,
98
+ outputs=outputs_image,
99
+ title=model_heading,
100
+ description=description,
101
+ examples=image_path,
102
+ article=article,
103
+ cache_examples=False
104
+ )
105
+
106
+ demo.css = """
107
+ .json-holder {
108
+ height: 300px;
109
+ overflow: auto;
110
+ }
111
+ """
112
+
113
+ demo.launch(share=False)
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cdff382908ed02720c5f80a3f519d9273ad3e5e01abba80b4b484f455f34c988
3
+ size 114391826
init.sh ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ wget https://iiif.dl.itc.u-tokyo.ac.jp/iiif/genji/TIFF/A00_6587/01/01_0004.tif/full/1024,/0/default.jpg -O "『源氏物語』(東京大学総合図書館所蔵).jpg"
2
+
3
+ wget https://rmda.kulib.kyoto-u.ac.jp/iiif/RB00007030/01/RB00007030_00003_0.ptif/full/1024,/0/default.jpg -O "『源氏物語』(京都大学所蔵).jpg"
4
+
5
+ wget https://kotenseki.nijl.ac.jp/api/iiif/100312034/v4/HRSM/HRSM-00396/HRSM-00396-00012.tif/full/1024,/0/default.jpg -O "『平家物語』(国文学研究資料館提供).jpg"
requirements-dev.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # --upgrade pip
2
+ -r requirements.txt
3
+ gradio
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ ultralytics
2
+ Pillow
/343/200/216/345/271/263/345/256/266/347/211/251/350/252/236/343/200/217(/345/233/275/346/226/207/345/255/246/347/240/224/347/251/266/350/263/207/346/226/231/351/244/250/346/217/220/344/276/233).jpg ADDED
/343/200/216/346/272/220/346/260/217/347/211/251/350/252/236/343/200/217(/344/272/254/351/203/275/345/244/247/345/255/246/346/211/200/350/224/265).jpg ADDED
/343/200/216/346/272/220/346/260/217/347/211/251/350/252/236/343/200/217(/346/235/261/344/272/254/345/244/247/345/255/246/347/267/217/345/220/210/345/233/263/346/233/270/351/244/250/346/211/200/350/224/265).jpg ADDED