mirror of
				https://github.com/StefBuwalda/ProjectIOT.git
				synced 2025-10-31 03:39:57 +00:00 
			
		
		
		
	refactoring + execution time
This commit is contained in:
		
							
								
								
									
										52
									
								
								server.py
									
									
									
									
									
								
							
							
						
						
									
										52
									
								
								server.py
									
									
									
									
									
								
							| @@ -1,22 +1,14 @@ | ||||
| from flask import request, jsonify | ||||
| from pyplatex import ANPR  # type: ignore | ||||
| from ultralytics.nn.tasks import DetectionModel  # type: ignore | ||||
| import os | ||||
| import torch | ||||
| import asyncio | ||||
| from application import app, db | ||||
| from application.models import LoggedItem, AllowedPlate | ||||
| from datetime import datetime | ||||
| from application import app | ||||
| from application.api.views import api_blueprint | ||||
|  | ||||
|  | ||||
| torch.serialization.add_safe_globals(  # type: ignore | ||||
|     {"DetectionModel": DetectionModel}  # type: ignore | ||||
| ) | ||||
|  | ||||
| anpr = ANPR() | ||||
|  | ||||
| # Saving images locally | ||||
| UPLOAD_FOLDER = "uploads" | ||||
| os.makedirs(UPLOAD_FOLDER, exist_ok=True) | ||||
| app.register_blueprint(api_blueprint) | ||||
|  | ||||
|  | ||||
| # Default app route | ||||
| @@ -25,41 +17,5 @@ def home(): | ||||
|     return "Hello, World!" | ||||
|  | ||||
|  | ||||
| # API to process vehicle | ||||
| @app.route("/api", methods=["POST"]) | ||||
| def data(): | ||||
|     data = request.data | ||||
|  | ||||
|     with open("image.jpg", "wb") as f: | ||||
|         f.write(data) | ||||
|  | ||||
|     status = asyncio.run(process_image(file="image.jpg", anpr=anpr)) | ||||
|     return jsonify( | ||||
|         { | ||||
|             "message": "Image sent succesfully", | ||||
|             "status": status, | ||||
|         } | ||||
|     ) | ||||
|  | ||||
|  | ||||
| async def process_image(file: str, anpr: ANPR) -> bool: | ||||
|     print("Processing image") | ||||
|     anpr_info = await anpr.detect(file)  # type: ignore | ||||
|     number_plate = anpr_info["plate_number"] | ||||
|     if number_plate: | ||||
|         allowed = ( | ||||
|             AllowedPlate.query.filter_by(plate=number_plate).first() | ||||
|             is not None | ||||
|         ) | ||||
|         db.session.add( | ||||
|             LoggedItem( | ||||
|                 plate=number_plate, allowed=allowed, datetime=datetime.now() | ||||
|             ) | ||||
|         ) | ||||
|         db.session.commit() | ||||
|         return allowed | ||||
|     return False | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|     app.run(host="192.168.137.1", port=2222, debug=True) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user