Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 7 |
+
import pytesseract
|
| 8 |
+
from ultralytics import YOLO
|
| 9 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 10 |
+
|
| 11 |
+
# Load CLIP model and processor
|
| 12 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 13 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 14 |
+
|
| 15 |
+
# Load YOLOv8 model
|
| 16 |
+
yolo = YOLO("yolov8x.pt") # change to yolov8n.pt for faster inference if needed
|
| 17 |
+
|
| 18 |
+
# Set Tesseract OCR path if needed
|
| 19 |
+
pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
|
| 20 |
+
|
| 21 |
+
def extract_clip_features(image):
|
| 22 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
features = model.get_image_features(**inputs)
|
| 25 |
+
return features / features.norm(p=2, dim=-1, keepdim=True)
|
| 26 |
+
|
| 27 |
+
def detect_text(image):
|
| 28 |
+
return pytesseract.image_to_string(image)
|
| 29 |
+
|
| 30 |
+
def detect_objects(image):
|
| 31 |
+
results = yolo(image)
|
| 32 |
+
boxes = results[0].boxes.xywh.cpu().numpy() # (x, y, w, h)
|
| 33 |
+
return boxes
|
| 34 |
+
|
| 35 |
+
def compare_images(img1, img2):
|
| 36 |
+
img1 = img1.convert("RGB")
|
| 37 |
+
img2 = img2.convert("RGB")
|
| 38 |
+
|
| 39 |
+
# Extract features
|
| 40 |
+
feat1 = extract_clip_features(img1)
|
| 41 |
+
feat2 = extract_clip_features(img2)
|
| 42 |
+
|
| 43 |
+
# Cosine similarity
|
| 44 |
+
sim_score = cosine_similarity(feat1, feat2)[0][0]
|
| 45 |
+
|
| 46 |
+
# OCR text similarity
|
| 47 |
+
text1 = detect_text(img1)
|
| 48 |
+
text2 = detect_text(img2)
|
| 49 |
+
|
| 50 |
+
vec1 = np.array([ord(c) for c in text1[:100]] + [0]*100)[:100]
|
| 51 |
+
vec2 = np.array([ord(c) for c in text2[:100]] + [0]*100)[:100]
|
| 52 |
+
text_sim = cosine_similarity([vec1], [vec2])[0][0]
|
| 53 |
+
|
| 54 |
+
# Object detection
|
| 55 |
+
obj1 = detect_objects(np.array(img1))
|
| 56 |
+
obj2 = detect_objects(np.array(img2))
|
| 57 |
+
shape_diff = abs(len(obj1) - len(obj2)) / max(len(obj1), 1)
|
| 58 |
+
|
| 59 |
+
# Geometric center distance (avg heuristic)
|
| 60 |
+
dist_penalty = 0
|
| 61 |
+
for i in range(min(len(obj1), len(obj2))):
|
| 62 |
+
dist_penalty += np.linalg.norm(obj1[i][:2] - obj2[i][:2])
|
| 63 |
+
dist_penalty /= max(len(obj1), 1)
|
| 64 |
+
|
| 65 |
+
# Final similarity score
|
| 66 |
+
final_score = (0.5 * sim_score) + (0.3 * text_sim) + (0.2 * (1 - shape_diff))
|
| 67 |
+
rating = round(final_score * 5, 2)
|
| 68 |
+
rating_clamped = min(5.0, max(0.0, rating))
|
| 69 |
+
|
| 70 |
+
return {
|
| 71 |
+
"Similarity Score (%)": f"{final_score*100:.2f}%",
|
| 72 |
+
"Rating (0–5)": f"{rating_clamped:.1f} ⭐",
|
| 73 |
+
"Text in Image 1": text1.strip()[:200],
|
| 74 |
+
"Text in Image 2": text2.strip()[:200],
|
| 75 |
+
"Detected Objects (img1, img2)": f"{len(obj1)} vs {len(obj2)}"
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
def gradio_ui(img1, img2):
|
| 79 |
+
result = compare_images(img1, img2)
|
| 80 |
+
return (
|
| 81 |
+
result["Similarity Score (%)"],
|
| 82 |
+
result["Rating (0–5)"],
|
| 83 |
+
result["Text in Image 1"],
|
| 84 |
+
result["Text in Image 2"],
|
| 85 |
+
result["Detected Objects (img1, img2)"]
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Gradio Interface
|
| 89 |
+
demo = gr.Interface(
|
| 90 |
+
fn=gradio_ui,
|
| 91 |
+
inputs=[
|
| 92 |
+
gr.Image(type="pil", label="Input Image"),
|
| 93 |
+
gr.Image(type="pil", label="Output Image")
|
| 94 |
+
],
|
| 95 |
+
outputs=[
|
| 96 |
+
gr.Text(label="Similarity Score (%)"),
|
| 97 |
+
gr.Text(label="Predicted Rating (0-5 Stars)"),
|
| 98 |
+
gr.Textbox(label="Extracted Text from Input"),
|
| 99 |
+
gr.Textbox(label="Extracted Text from Output"),
|
| 100 |
+
gr.Text(label="Part/Object Count Comparison")
|
| 101 |
+
],
|
| 102 |
+
title="🛠️ CAD Image Comparison AI",
|
| 103 |
+
description="Upload two CAD/Technical images and get a full feature-based similarity score including shapes, geometry, dimensions, and text."
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
demo.launch()
|