Update app.py
Browse files
app.py
CHANGED
|
@@ -7,101 +7,62 @@ from ultralytics import YOLO
|
|
| 7 |
from PIL import Image
|
| 8 |
import uvicorn
|
| 9 |
|
| 10 |
-
|
| 11 |
-
app = FastAPI(title="YOLO + GIT Large Detailed API")
|
| 12 |
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
MY_MODEL_PATH = 'best.pt'
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# تحميل YOLO
|
| 19 |
try:
|
| 20 |
detection_model = YOLO(MY_MODEL_PATH)
|
| 21 |
-
|
| 22 |
-
except Exception as e:
|
| 23 |
-
print(f"⚠️ فشل تحميل الموديل الخاص، استخدام الافتراضي: {e}")
|
| 24 |
detection_model = YOLO("yolov8n.pt")
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
processor = AutoProcessor.from_pretrained(model_name)
|
| 29 |
-
caption_model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
| 30 |
-
print(f"✅ تم تحميل {model_name} بنجاح")
|
| 31 |
-
|
| 32 |
-
@app.get("/")
|
| 33 |
-
def home():
|
| 34 |
-
return {"status": "Online", "mode": "Detailed Color & Shape Description"}
|
| 35 |
-
|
| 36 |
-
# --- 2. وظيفة المعالجة ---
|
| 37 |
|
| 38 |
@app.post("/analyze")
|
| 39 |
async def analyze_image(file: UploadFile = File(...)):
|
| 40 |
data = await file.read()
|
| 41 |
original_image = Image.open(io.BytesIO(data)).convert("RGB")
|
| 42 |
|
| 43 |
-
# الكشف عن الأجسام
|
| 44 |
results = detection_model(original_image, conf=0.25)
|
| 45 |
integrated_results = []
|
| 46 |
|
| 47 |
for r in results:
|
| 48 |
for i, box in enumerate(r.boxes):
|
| 49 |
label = r.names[int(box.cls)]
|
| 50 |
-
conf_score = float(box.conf[0])
|
| 51 |
coords = box.xyxy[0].tolist()
|
| 52 |
|
| 53 |
-
# قص العنصر مع هامش
|
| 54 |
-
pad =
|
| 55 |
cropped_img = original_image.crop((
|
| 56 |
-
max(0, coords[0]-pad),
|
| 57 |
-
|
| 58 |
-
min(original_image.width, coords[2]+pad),
|
| 59 |
-
min(original_image.height, coords[3]+pad)
|
| 60 |
))
|
| 61 |
|
| 62 |
-
# --- ال
|
| 63 |
-
# ن
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
inputs = processor(images=cropped_img, text=prompt, return_tensors="pt").to(device)
|
| 67 |
|
| 68 |
generated_ids = caption_model.generate(
|
| 69 |
pixel_values=inputs.pixel_values,
|
| 70 |
-
|
| 71 |
-
max_new_tokens=40, # نطلب توليد كلمات جديدة فقط
|
| 72 |
num_beams=5,
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 79 |
-
|
| 80 |
-
# تنظيف النتيجة: إزالة البرومبت إذا ظهر في البداية
|
| 81 |
-
if full_text.startswith(prompt):
|
| 82 |
-
description_only = full_text[len(prompt):].strip()
|
| 83 |
-
else:
|
| 84 |
-
description_only = full_text.strip()
|
| 85 |
|
| 86 |
integrated_results.append({
|
| 87 |
"object_id": i + 1,
|
| 88 |
"label": label,
|
| 89 |
-
"
|
| 90 |
-
"visual_description": f"The {label} is {description_only}"
|
| 91 |
})
|
| 92 |
|
| 93 |
-
|
| 94 |
-
if not integrated_results:
|
| 95 |
-
inputs = processor(images=original_image, return_tensors="pt").to(device)
|
| 96 |
-
generated_ids = caption_model.generate(pixel_values=inputs.pixel_values, max_new_tokens=40)
|
| 97 |
-
desc = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 98 |
-
return {"message": "No specific objects detected", "general_description": desc}
|
| 99 |
-
|
| 100 |
-
return {
|
| 101 |
-
"detected_count": len(integrated_results),
|
| 102 |
-
"results": integrated_results
|
| 103 |
-
}
|
| 104 |
|
| 105 |
-
# --- 3. تشغيل السيرفر ---
|
| 106 |
if __name__ == "__main__":
|
| 107 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
import uvicorn
|
| 9 |
|
| 10 |
+
app = FastAPI(title="YOLO + GIT Large: Color & Shape Edition")
|
|
|
|
| 11 |
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
MY_MODEL_PATH = 'best.pt'
|
| 14 |
|
| 15 |
+
# تحميل الموديلات
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
detection_model = YOLO(MY_MODEL_PATH)
|
| 18 |
+
except:
|
|
|
|
|
|
|
| 19 |
detection_model = YOLO("yolov8n.pt")
|
| 20 |
|
| 21 |
+
processor = AutoProcessor.from_pretrained("microsoft/git-large")
|
| 22 |
+
caption_model = AutoModelForCausalLM.from_pretrained("microsoft/git-large").to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
@app.post("/analyze")
|
| 25 |
async def analyze_image(file: UploadFile = File(...)):
|
| 26 |
data = await file.read()
|
| 27 |
original_image = Image.open(io.BytesIO(data)).convert("RGB")
|
| 28 |
|
|
|
|
| 29 |
results = detection_model(original_image, conf=0.25)
|
| 30 |
integrated_results = []
|
| 31 |
|
| 32 |
for r in results:
|
| 33 |
for i, box in enumerate(r.boxes):
|
| 34 |
label = r.names[int(box.cls)]
|
|
|
|
| 35 |
coords = box.xyxy[0].tolist()
|
| 36 |
|
| 37 |
+
# قص العنصر مع هامش (Padding) لرؤية الشكل واللون بوضوح
|
| 38 |
+
pad = 10
|
| 39 |
cropped_img = original_image.crop((
|
| 40 |
+
max(0, coords[0]-pad), max(0, coords[1]-pad),
|
| 41 |
+
min(original_image.width, coords[2]+pad), min(original_image.height, coords[3]+pad)
|
|
|
|
|
|
|
| 42 |
))
|
| 43 |
|
| 44 |
+
# --- التعديل الجوهري للوصف ---
|
| 45 |
+
# نستخدم برومبت مفتوح ليقوم الموديل بالوصف التلقائي
|
| 46 |
+
inputs = processor(images=cropped_img, return_tensors="pt").to(device)
|
|
|
|
|
|
|
| 47 |
|
| 48 |
generated_ids = caption_model.generate(
|
| 49 |
pixel_values=inputs.pixel_values,
|
| 50 |
+
max_new_tokens=50,
|
|
|
|
| 51 |
num_beams=5,
|
| 52 |
+
do_sample=True, # تفعيل التنوع في الكلمات
|
| 53 |
+
temperature=0.8, # درجة "إبداع" لوصف الألوان بدقة
|
| 54 |
+
repetition_penalty=1.2
|
| 55 |
)
|
| 56 |
|
| 57 |
+
description = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
integrated_results.append({
|
| 60 |
"object_id": i + 1,
|
| 61 |
"label": label,
|
| 62 |
+
"visual_description": f"This {label} is {description}"
|
|
|
|
| 63 |
})
|
| 64 |
|
| 65 |
+
return {"results": integrated_results}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
|
|
|
| 67 |
if __name__ == "__main__":
|
| 68 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|