Spaces:
Configuration error
Configuration error
Update app.py
#6
by
BenK0y
- opened
app.py
CHANGED
|
@@ -1,77 +1,30 @@
|
|
| 1 |
-
import
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
# En-têtes de la requête
|
| 7 |
-
headers = {
|
| 8 |
-
'Content-Type': 'application/json',
|
| 9 |
-
}
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
"contents": [
|
| 14 |
-
{
|
| 15 |
-
"parts": [
|
| 16 |
-
{"text": "Explain how AI works"}
|
| 17 |
-
]
|
| 18 |
-
}
|
| 19 |
-
]
|
| 20 |
-
}
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
import torch
|
| 33 |
|
| 34 |
-
|
| 35 |
-
ocr_tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
| 36 |
-
ocr_model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, device_map='cuda', use_safetensors=True)
|
| 37 |
-
ocr_model.eval().cuda()
|
| 38 |
-
|
| 39 |
-
# Charger le modèle de suivi et reconnaissance de couleur des véhicules
|
| 40 |
-
vehicle_model = torch.hub.load('ultralytics/yolov5', 'custom', path='sujithvamshi/Real-Time-Vehicle-Tracking-And-Colour-Recognition/best.pt')
|
| 41 |
-
|
| 42 |
-
# Fonction pour extraire la plaque d'immatriculation avec OCR
|
| 43 |
-
def get_license_plate(image):
|
| 44 |
-
# Utiliser le modèle GOT-OCR pour extraire le texte (plaque d'immatriculation)
|
| 45 |
-
image_tensor = ocr_tokenizer(image, return_tensors="pt").input_ids
|
| 46 |
-
with torch.no_grad():
|
| 47 |
-
output = ocr_model(image_tensor)
|
| 48 |
-
plate_text = ocr_tokenizer.decode(output.logits[0], skip_special_tokens=True)
|
| 49 |
-
return plate_text
|
| 50 |
-
|
| 51 |
-
# Fonction pour extraire la couleur du véhicule
|
| 52 |
-
def get_vehicle_color(image):
|
| 53 |
-
# Utiliser le modèle Real-Time-Vehicle-Tracking-And-Colour-Recognition pour obtenir la couleur
|
| 54 |
-
results = vehicle_model(image)
|
| 55 |
-
color_info = results.pandas().xyxy[0].color # Hypothèse: le modèle retourne une info de couleur
|
| 56 |
-
return color_info
|
| 57 |
-
|
| 58 |
-
# Fusionner les deux résultats
|
| 59 |
-
def process_image(image_path):
|
| 60 |
-
image = Image.open(image_path)
|
| 61 |
-
|
| 62 |
-
# 1. Extraire la plaque d'immatriculation
|
| 63 |
-
license_plate = get_license_plate(image)
|
| 64 |
-
|
| 65 |
-
# 2. Extraire la couleur du véhicule
|
| 66 |
-
vehicle_color = get_vehicle_color(image)
|
| 67 |
-
|
| 68 |
-
# 3. Retourner la fusion des résultats
|
| 69 |
-
return {
|
| 70 |
-
"license_plate": license_plate,
|
| 71 |
-
"vehicle_color": vehicle_color
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
# Exemple d'utilisation
|
| 75 |
-
image_path = "path_to_your_image.jpg"
|
| 76 |
-
result = process_image(image_path)
|
| 77 |
-
print(f"Plaque d'immatriculation: {result['license_plate']}, Couleur du véhicule: {result['vehicle_color']}")
|
|
|
|
| 1 |
+
from transformers import AutoModel, AutoTokenizer
|
| 2 |
|
| 3 |
+
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
| 4 |
+
model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, device_map='cuda', use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
|
| 5 |
+
model = model.eval().cuda()
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# input your test image
|
| 9 |
+
image_file = 'car.jpg'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# plain texts OCR
|
| 12 |
+
res = model.chat(tokenizer, image_file, ocr_type='ocr')
|
| 13 |
|
| 14 |
+
# format texts OCR:
|
| 15 |
+
# res = model.chat(tokenizer, image_file, ocr_type='format')
|
| 16 |
|
| 17 |
+
# fine-grained OCR:
|
| 18 |
+
# res = model.chat(tokenizer, image_file, ocr_type='ocr', ocr_box='')
|
| 19 |
+
# res = model.chat(tokenizer, image_file, ocr_type='format', ocr_box='')
|
| 20 |
+
# res = model.chat(tokenizer, image_file, ocr_type='ocr', ocr_color='')
|
| 21 |
+
# res = model.chat(tokenizer, image_file, ocr_type='format', ocr_color='')
|
| 22 |
|
| 23 |
+
# multi-crop OCR:
|
| 24 |
+
# res = model.chat_crop(tokenizer, image_file, ocr_type='ocr')
|
| 25 |
+
# res = model.chat_crop(tokenizer, image_file, ocr_type='format')
|
| 26 |
|
| 27 |
+
# render the formatted OCR results:
|
| 28 |
+
# res = model.chat(tokenizer, image_file, ocr_type='format', render=True, save_render_file = './demo.html')
|
|
|
|
| 29 |
|
| 30 |
+
print(res)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|