muhammadhamza-stack commited on
Commit
840c7b5
·
1 Parent(s): 1a49165

clean the app code and remove example results cashe

Browse files
Files changed (1) hide show
  1. app.py +1 -114
app.py CHANGED
@@ -1,116 +1,3 @@
1
- # import cv2
2
- # import numpy as np
3
- # from PIL import Image
4
- # import torch
5
- # from torchvision import models, transforms
6
- # from ultralytics import YOLO
7
- # import gradio as gr
8
- # import torch.nn as nn
9
-
10
- # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
11
-
12
- # # Load models
13
- # yolo_model = YOLO('best.pt') # Make sure this file is uploaded
14
- # resnet = models.resnet50(pretrained=False)
15
- # resnet.fc = nn.Linear(resnet.fc.in_features, 3)
16
- # resnet.load_state_dict(torch.load('rice_resnet_model.pth', map_location=device))
17
- # resnet = resnet.to(device)
18
- # resnet.eval()
19
-
20
- # # Class labels
21
- # class_labels = ["c9", "kant", "superf"]
22
-
23
- # # Image transformations
24
- # transform = transforms.Compose([
25
- # transforms.Resize((224, 224)),
26
- # transforms.ToTensor(),
27
- # transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
28
- # ])
29
-
30
- # def classify_crop(crop_img):
31
- # """ایک چاول کے دانے کو درجہ بند کریں"""
32
- # image = transform(crop_img).unsqueeze(0).to(device)
33
- # with torch.no_grad():
34
- # output = resnet(image)
35
- # _, predicted = torch.max(output, 1)
36
- # return class_labels[predicted.item()]
37
-
38
- # def detect_and_classify(input_image):
39
- # """تصویر پر کارروائی کریں اور ہر دانے کو شناخت کریں"""
40
- # image = np.array(input_image)
41
- # image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
42
-
43
- # results = yolo_model(image)[0]
44
- # boxes = results.boxes.xyxy.cpu().numpy()
45
-
46
- # for box in boxes:
47
- # x1, y1, x2, y2 = map(int, box[:4])
48
- # crop = image[y1:y2, x1:x2]
49
- # crop_pil = Image.fromarray(cv2.cvtColor(crop, cv2.COLOR_BGR2RGB))
50
- # predicted_label = classify_crop(crop_pil)
51
-
52
- # cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
53
- # cv2.putText(image,
54
- # predicted_label,
55
- # (x1, y1-10),
56
- # cv2.FONT_HERSHEY_SIMPLEX,
57
- # 0.9,
58
- # (36, 255, 12),
59
- # 2)
60
-
61
- # return Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
62
-
63
- # with gr.Blocks(title="چاول کی اقسام کی درجہ بندی") as demo:
64
- # gr.Markdown("""
65
- # ## 🍚 چاول کی اقسام کی شناخت کا نظام
66
- # ایک تصویر اپ لوڈ کریں جس میں چاول کے دانے ہوں۔
67
- # سسٹم ہر دانے کو شناخت اور درجہ بند کرے گا۔
68
- # """)
69
-
70
- # with gr.Row():
71
- # with gr.Column():
72
- # image_input = gr.Image(type="pil", label="چاول کی تصویر اپ لوڈ کریں")
73
- # submit_btn = gr.Button("تجزیہ شروع کریں", variant="primary")
74
- # with gr.Column():
75
- # output_image = gr.Image(label="نتائج", interactive=False)
76
-
77
- # submit_btn.click(
78
- # fn=detect_and_classify,
79
- # inputs=image_input,
80
- # outputs=output_image
81
- # )
82
-
83
- # # ✅ Move this block inside the `with gr.Blocks(...)` scope
84
- # gr.Examples(
85
- # examples=[
86
- # "samples/rice1.jpg",
87
- # "samples/rice2.jpg",
88
- # "samples/rice3.jpg",
89
- # "samples/rice4.jpg",
90
- # "samples/rice5.jpg",
91
- # "samples/rice6.jpg"
92
- # ],
93
- # inputs=image_input,
94
- # label="مثال تصاویر"
95
- # )
96
- # gr.Markdown("""
97
- # ### ℹ️ ہدایات:
98
- # - ✅ واضح اور الگ الگ چاول کے دانے والی تصویر اپ لوڈ کریں۔
99
- # - ⚠️ اگر دانے آپس میں جُڑے ہوں یا ایک دوسرے پر چڑھے ہوں، تو نتائج متاثر ہو سکتے ہیں۔
100
- # - 📸 بہتر پہچان کے لیے تصویر کا پس منظر صاف اور دانے منتشر (پھیلے ہوئے) ہونے چاہئیں۔
101
- # - 🖼️ آپ اوپر دی گئی مثال تصاویر کو بھی دیکھ سکتے ہیں۔
102
- # """)
103
-
104
-
105
-
106
- # demo.launch()
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
  import cv2
115
  import numpy as np
116
  from PIL import Image
@@ -301,7 +188,7 @@ with gr.Blocks(title="Rice Variety Classification") as demo:
301
  inputs=image_input,
302
  outputs=output_image, # Required for proper caching and execution
303
  fn=detect_and_classify, # Required for proper caching and execution
304
- cache_examples=True,
305
  label="Click to load and run a sample image / نمونہ تصویر لوڈ اور رن کرنے کے لیے کلک کریں"
306
  )
307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import cv2
2
  import numpy as np
3
  from PIL import Image
 
188
  inputs=image_input,
189
  outputs=output_image, # Required for proper caching and execution
190
  fn=detect_and_classify, # Required for proper caching and execution
191
+ # cache_examples=True,
192
  label="Click to load and run a sample image / نمونہ تصویر لوڈ اور رن کرنے کے لیے کلک کریں"
193
  )
194