Spaces:
Sleeping
Sleeping
Testing new brain
Browse filesTesting paddy disease images With new brain
test.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from glob import glob
|
| 5 |
+
from tqdm import tqdm
|
| 6 |
+
|
| 7 |
+
# 1. INPUT: Your seed data (Original 31 images)
|
| 8 |
+
INPUT_FOLDER = r"C:\Users\charu\Desktop\seed_data"
|
| 9 |
+
|
| 10 |
+
# 2. OUTPUT: Where to save the checked images
|
| 11 |
+
OUTPUT_FOLDER = r"C:\Users\charu\Desktop\seed_verification_output"
|
| 12 |
+
|
| 13 |
+
def verify_and_save():
|
| 14 |
+
if not os.path.exists(OUTPUT_FOLDER):
|
| 15 |
+
os.makedirs(OUTPUT_FOLDER)
|
| 16 |
+
|
| 17 |
+
print(f"🕵️ Checking data in: {INPUT_FOLDER}")
|
| 18 |
+
txt_files = glob(os.path.join(INPUT_FOLDER, "*.txt"))
|
| 19 |
+
|
| 20 |
+
if not txt_files:
|
| 21 |
+
print("❌ No text files found! Check your path.")
|
| 22 |
+
return
|
| 23 |
+
|
| 24 |
+
print(f" -> Found {len(txt_files)} files. Drawing polygons...")
|
| 25 |
+
|
| 26 |
+
for txt_path in tqdm(txt_files):
|
| 27 |
+
# Load Image
|
| 28 |
+
filename = os.path.basename(txt_path).replace('.txt', '')
|
| 29 |
+
img_path_jpg = os.path.join(INPUT_FOLDER, filename + ".jpg")
|
| 30 |
+
img_path_png = os.path.join(INPUT_FOLDER, filename + ".png")
|
| 31 |
+
img_path_jpeg = os.path.join(INPUT_FOLDER, filename + ".jpeg")
|
| 32 |
+
|
| 33 |
+
if os.path.exists(img_path_jpg): img_path = img_path_jpg
|
| 34 |
+
elif os.path.exists(img_path_png): img_path = img_path_png
|
| 35 |
+
elif os.path.exists(img_path_jpeg): img_path = img_path_jpeg
|
| 36 |
+
else:
|
| 37 |
+
print(f"⚠️ Image missing for {filename}")
|
| 38 |
+
continue
|
| 39 |
+
|
| 40 |
+
img = cv2.imread(img_path)
|
| 41 |
+
if img is None: continue
|
| 42 |
+
h, w, _ = img.shape
|
| 43 |
+
|
| 44 |
+
# Load Labels
|
| 45 |
+
with open(txt_path, 'r') as f:
|
| 46 |
+
lines = f.readlines()
|
| 47 |
+
|
| 48 |
+
for line in lines:
|
| 49 |
+
parts = line.strip().split()
|
| 50 |
+
if not parts: continue
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
cls_id = int(parts[0])
|
| 54 |
+
|
| 55 |
+
# Parse Polygons
|
| 56 |
+
coords = [float(x) for x in parts[1:]]
|
| 57 |
+
|
| 58 |
+
# Check format (Is it Box or Polygon?)
|
| 59 |
+
if len(coords) == 4:
|
| 60 |
+
# It is a BOX (x, y, w, h) -> Draw Rectangle
|
| 61 |
+
cx, cy, bw, bh = coords
|
| 62 |
+
x1 = int((cx - bw/2) * w)
|
| 63 |
+
y1 = int((cy - bh/2) * h)
|
| 64 |
+
x2 = int((cx + bw/2) * w)
|
| 65 |
+
y2 = int((cy + bh/2) * h)
|
| 66 |
+
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 3)
|
| 67 |
+
label_text = "BOX"
|
| 68 |
+
pt = (x1, y1 - 10)
|
| 69 |
+
else:
|
| 70 |
+
# It is a POLYGON -> Draw Shape
|
| 71 |
+
points = []
|
| 72 |
+
for i in range(0, len(coords), 2):
|
| 73 |
+
px = int(coords[i] * w)
|
| 74 |
+
py = int(coords[i+1] * h)
|
| 75 |
+
points.append([px, py])
|
| 76 |
+
|
| 77 |
+
pts = np.array(points, np.int32).reshape((-1, 1, 2))
|
| 78 |
+
cv2.polylines(img, [pts], isClosed=True, color=(0, 255, 0), thickness=2)
|
| 79 |
+
label_text = "POLY"
|
| 80 |
+
pt = tuple(points[0])
|
| 81 |
+
|
| 82 |
+
# Add Text
|
| 83 |
+
cv2.putText(img, f"{cls_id}-{label_text}", pt, cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Error parsing {filename}: {e}")
|
| 86 |
+
|
| 87 |
+
# Save to Output Folder
|
| 88 |
+
save_path = os.path.join(OUTPUT_FOLDER, filename + "_check.jpg")
|
| 89 |
+
cv2.imwrite(save_path, img)
|
| 90 |
+
|
| 91 |
+
print(f"\n✅ Done! Go open this folder and look at the images:")
|
| 92 |
+
print(f" {OUTPUT_FOLDER}")
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
verify_and_save()
|