hafizarslan commited on
Commit
6460e33
·
verified ·
1 Parent(s): 6782d24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -29,7 +29,7 @@ transform = transforms.Compose([
29
  ])
30
 
31
  def classify_crop(crop_img):
32
- """Classify a single rice grain"""
33
  image = transform(crop_img).unsqueeze(0).to(device)
34
  with torch.no_grad():
35
  output = resnet(image)
@@ -37,23 +37,19 @@ def classify_crop(crop_img):
37
  return class_labels[predicted.item()]
38
 
39
  def detect_and_classify(input_image):
40
- """Process uploaded image"""
41
- # Convert Gradio Image to OpenCV format
42
  image = np.array(input_image)
43
  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
44
-
45
- # YOLO Detection
46
  results = yolo_model(image)[0]
47
  boxes = results.boxes.xyxy.cpu().numpy()
48
-
49
- # Process each detection
50
  for box in boxes:
51
  x1, y1, x2, y2 = map(int, box[:4])
52
  crop = image[y1:y2, x1:x2]
53
  crop_pil = Image.fromarray(cv2.cvtColor(crop, cv2.COLOR_BGR2RGB))
54
  predicted_label = classify_crop(crop_pil)
55
 
56
- # Draw bounding box and label
57
  cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
58
  cv2.putText(image,
59
  predicted_label,
@@ -63,22 +59,22 @@ def detect_and_classify(input_image):
63
  (36, 255, 12),
64
  2)
65
 
66
- # Convert back to RGB for Gradio
67
  return Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
68
 
69
- # Create Gradio interface
70
- with gr.Blocks(title="Rice Classification") as demo:
71
  gr.Markdown("""
72
- ## 🍚 Rice Variety Classifier
73
- Upload an image containing rice grains. The system will detect and classify each grain.
 
74
  """)
75
 
76
  with gr.Row():
77
  with gr.Column():
78
- image_input = gr.Image(type="pil", label="Upload Rice Image")
79
- submit_btn = gr.Button("Analyze", variant="primary")
80
  with gr.Column():
81
- output_image = gr.Image(label="Detection Results", interactive=False)
82
 
83
  submit_btn.click(
84
  fn=detect_and_classify,
@@ -86,5 +82,5 @@ with gr.Blocks(title="Rice Classification") as demo:
86
  outputs=output_image
87
  )
88
 
89
- # Launch the app
90
- demo.launch()
 
29
  ])
30
 
31
  def classify_crop(crop_img):
32
+ """ایک چاول کے دانے کو درجہ بند کریں"""
33
  image = transform(crop_img).unsqueeze(0).to(device)
34
  with torch.no_grad():
35
  output = resnet(image)
 
37
  return class_labels[predicted.item()]
38
 
39
  def detect_and_classify(input_image):
40
+ """تصویر پر کارروائی کریں اور ہر دانے کو شناخت کریں"""
 
41
  image = np.array(input_image)
42
  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
43
+
 
44
  results = yolo_model(image)[0]
45
  boxes = results.boxes.xyxy.cpu().numpy()
46
+
 
47
  for box in boxes:
48
  x1, y1, x2, y2 = map(int, box[:4])
49
  crop = image[y1:y2, x1:x2]
50
  crop_pil = Image.fromarray(cv2.cvtColor(crop, cv2.COLOR_BGR2RGB))
51
  predicted_label = classify_crop(crop_pil)
52
 
 
53
  cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
54
  cv2.putText(image,
55
  predicted_label,
 
59
  (36, 255, 12),
60
  2)
61
 
 
62
  return Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
63
 
64
+ # Gradio انٹرفیس بنائیں
65
+ with gr.Blocks(title="چاول کی اقسام کی درجہ بندی") as demo:
66
  gr.Markdown("""
67
+ ## 🍚 چاول کی اقسام کی شناخت کا نظام
68
+ ایک تصویر اپ لوڈ کریں جس میں چاول کے دانے ہوں۔
69
+ سسٹم ہر دانے کو شناخت اور درجہ بند کرے گا۔
70
  """)
71
 
72
  with gr.Row():
73
  with gr.Column():
74
+ image_input = gr.Image(type="pil", label="چاول کی تصویر اپ لوڈ کریں")
75
+ submit_btn = gr.Button("تجزیہ شروع کریں", variant="primary")
76
  with gr.Column():
77
+ output_image = gr.Image(label="نتائج", interactive=False)
78
 
79
  submit_btn.click(
80
  fn=detect_and_classify,
 
82
  outputs=output_image
83
  )
84
 
85
+ # ایپ لانچ کریں
86
+ demo.launch()