MvitHYF commited on
Commit
080e73a
·
verified ·
1 Parent(s): 63b8183

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +133 -0
  2. gradio.css +8 -0
  3. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #app6.py
2
+ import gradio as gr
3
+ import torch
4
+ from PIL import Image
5
+ from torchvision import transforms
6
+ from ultralyticsplus import YOLO, render_result
7
+ # import matplotlib.pyplot as plt
8
+ import numpy as np
9
+
10
+ torch.hub.download_url_to_file(
11
+ 'https://i.postimg.cc/Y0V4KwMf/NSTA-Test-IMG-3276.jpg', 'A.jpg')
12
+ torch.hub.download_url_to_file(
13
+ 'https://i.postimg.cc/pTshCFSS/NSTB-Test-IMG-1472.jpg', 'B.jpg')
14
+ torch.hub.download_url_to_file(
15
+ 'https://i.postimg.cc/mkhc5rfg/NSTC-Test-IMG-0118.jpg', 'C.jpg')
16
+
17
+ def detect_objects(image_path):
18
+ # Open the image file and resize it
19
+ image = Image.open(image_path)
20
+ resized_image = image.resize((1024, 768))
21
+
22
+ # Load the model
23
+ # model_path = ('code/runs/train45/best.pt')
24
+ model_path = ('MvitHYF/v8mvitcocoaseed2024')
25
+ model = YOLO(model_path)
26
+ # model = YOLO('MvitHYF/v8mvitcocoaseed2024')
27
+
28
+ # Set model parameters
29
+ model.overrides['conf'] = 0.35 # NMS confidence threshold
30
+ model.overrides['iou'] = 0.45 # NMS IoU threshold
31
+ model.overrides['agnostic_nms'] = False # NMS class-agnostic
32
+ model.overrides['max_det'] = 1000 # maximum number of detections per image
33
+
34
+ # Perform inference
35
+ results = model.predict(resized_image)
36
+
37
+ #debug check count
38
+ # print("see")
39
+ # print(results)
40
+ cls = results[0].boxes.cls
41
+ # print(cls)
42
+ strcls = str(cls)
43
+ # print(type(strcls))
44
+ # print(strcls)
45
+ count_classa = strcls.count('0')
46
+ # print('Count of classA:', count_classa)
47
+ count_classb = strcls.count('1')
48
+ # print('Count of classB', count_classb)
49
+ count_classc = strcls.count('2')
50
+ # print('Count of classC:', count_classc)
51
+ intcount_classa = int(count_classa)
52
+ intcount_classb = int(count_classb)
53
+ intcount_classc = int(count_classc)
54
+ total = intcount_classa + intcount_classb + intcount_classc
55
+ # print("end see")
56
+
57
+ # class_counts = {'A': 0, 'B': 0, 'C': 0}
58
+
59
+ # for result in results:
60
+ # # Example to access class_id, adapt based on your results structure
61
+ # class_id = result[-1]
62
+ # if class_id == 0: # if the class_id corresponds to class A
63
+ # class_counts['A'] += 1
64
+ # elif class_id == 1: # if the class_id corresponds to class B
65
+ # class_counts['B'] += 1
66
+ # elif class_id == 2: # if the class_id corresponds to class C
67
+ # class_counts['C'] += 1
68
+
69
+ #plot graph
70
+ # plot = np.array([intcount_classa, intcount_classb, intcount_classc])
71
+ # piegraph = plt.pie(plot)
72
+
73
+ # x = np.array(["A", "B", "C"])
74
+ # y = np.array([intcount_classa, intcount_classb, intcount_classc])
75
+ # plotbar = plt.bar(x,y)
76
+
77
+ gr.Image(label="Pie Graph")
78
+ # Format the output to print the counts
79
+ output_counts = f"Totoal cocoa seeds: {total}\nClass A: {count_classa} seeds\nClass B: {count_classb} seeds\nClass C: {count_classc} seeds"
80
+
81
+ # Render results
82
+ render = render_result(model=model, image=resized_image, result=results[0])
83
+ #return render, output_counts, plotbar
84
+ return render, output_counts
85
+
86
+ #csspath = 'code/yolov8newultlt/gradio.css'
87
+
88
+ with gr.Blocks(theme='ParityError/LimeFace') as demo:
89
+ with gr.Row(): #original Column
90
+ with gr.Row(): #original Column
91
+ example = [['A.jpg'],
92
+ ['B.jpg'],
93
+ ['C.jpg']]
94
+
95
+ with gr.Row():
96
+ with gr.Column():
97
+ gr.Interface(fn=detect_objects,
98
+ inputs=gr.Image(type="filepath", label="Upload an Image"),
99
+ outputs=[gr.Image(type="filepath", label="Result"), gr.Textbox(label="Detection Counts")],
100
+ title="YOLOv8 Cocoas Seed Classification",
101
+ description="Upload an image to detect objects using YOLO.",
102
+ #html = gr.HTML(value="<p>This is another paragraph123.</p>"),
103
+ examples = example,
104
+ #css=csspath,
105
+ )
106
+ with gr.Row():
107
+ with gr.Row():
108
+ # gr.HTML(value="<b>Class A</b> <p>Class A is the best from all 3 classes. It have the best of physical appreance eg. shape, size, texture</p>"),
109
+ gr.HTML(value="<dl> <dt><b>Class A</b></dt> <dd>Class A is the best from all 3 classes. It have the best of physical appreance eg. shape, size, texture</dd> </dl> <dt><b>Class B</b></dt> <dd>Class B most of the cocoa seed have physical appreance similar to class A. <br> But the size must me smaller and texture is not smmoth as class A</dd> <dt><b>Class C</b></dt> <dd>Class C is the worst from all 3 classes. Its the smallest, rough texter and have a irregular shape </dd> </dl></dl>")
110
+
111
+ if __name__ == "__main__":
112
+ demo.queue().launch(share=True)
113
+
114
+ # def load_css():
115
+ # with open(csspath, 'r') as file:
116
+ # css_content = file.read()
117
+ # return css_content
118
+
119
+
120
+ # Create the Gradio interface
121
+ # iface = gr.Interface(fn=detect_objects,
122
+ # inputs=gr.Image(type="filepath", label="Upload an Image"),
123
+ # outputs=gr.Image(type="filepath", label="Result"),
124
+ # description="Upload an image to detect objects using YOLO.",
125
+ # title="YOLOv8 Cocoa Seed Classification",
126
+ # examples=example,
127
+ # theme='ParityError/LimeFace',
128
+ # #css=load_css()
129
+ # )
130
+
131
+ # Launch the interface
132
+ # iface.launch()
133
+ # demo.queue().launch()
gradio.css ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ /* gradio.css */
2
+
3
+ /* Target the images within the gradio app */
4
+ img {
5
+ width: 1024px; /* Set width directly */
6
+ height: 768px; /* Set height directly */
7
+ margin: 10px; /* Optional: add some space around the images */
8
+ }
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio==4.22.0
2
+ numpy==1.26.4
3
+ Pillow==10.2.0
4
+ torch==2.0.1
5
+ torchvision==0.15.2
6
+ ultralyticsplus==0.0.29