diff --git a/application/api/image_processing.py b/application/api/image_processing.py index bf05110..2f59340 100644 --- a/application/api/image_processing.py +++ b/application/api/image_processing.py @@ -20,11 +20,15 @@ async def process_image(image: bytes) -> str: if r.boxes: for box in r.boxes: cls_name = r.names[int(box.cls[0])] - if cls_name == "car": + if cls_name in ["car", "truck"]: x1, y1, x2, y2 = map(int, box.xyxy[0]) size = (x2 - x1) ** 2 + (y2 - y1) ** 2 cars.append((size, (x1, y1, x2, y2))) + else: + return "" + if cars == []: + return "" # Get the biggest car box size, corners = max(cars, key=lambda x: x[0]) @@ -45,5 +49,7 @@ async def process_image(image: bytes) -> str: lp_np = np.array(object=lp_img) result = ocr_reader.readtext(image=lp_np) print(result) + else: + return "" return str(result[0][1]) # type: ignore diff --git a/application/api/views.py b/application/api/views.py index e1e4088..41e08f4 100644 --- a/application/api/views.py +++ b/application/api/views.py @@ -17,13 +17,14 @@ def data(): application.last_image.seek(0) application.last_image = io.BytesIO(request.data) np = asyncio.run(process_image(image=data)) - - # Check if the found plate is allowed to exit - allowed = AllowedPlate.query.filter_by(plate=np).first() is not None - db.session.add( # Log the found numberplate and status - LoggedItem(plate=np, allowed=allowed, datetime=datetime.now()) - ) - db.session.commit() + allowed = False + if np != "": + # Check if the found plate is allowed to exit + allowed = AllowedPlate.query.filter_by(plate=np).first() is not None + db.session.add( # Log the found numberplate and status + LoggedItem(plate=np, allowed=allowed, datetime=datetime.now()) + ) + db.session.commit() # Return decision to Pico return jsonify( diff --git a/application/dashboard/views.py b/application/dashboard/views.py index b86d6c2..8cb1dd6 100644 --- a/application/dashboard/views.py +++ b/application/dashboard/views.py @@ -23,8 +23,10 @@ def dashboard(): .limit(50) .all() ) - form = npForm() - return render_template("dashboard.html", plates=Plates, logs=logs, form=form) + form = npForm() + return render_template( + "dashboard.html", plates=Plates, logs=logs, form=form + ) @dash_blueprint.route("/add", methods=["GET", "POST"]) diff --git a/car_crop_pillow.jpg b/car_crop_pillow.jpg index 447682a..a815195 100644 Binary files a/car_crop_pillow.jpg and b/car_crop_pillow.jpg differ diff --git a/license_plate.jpg b/license_plate.jpg index c6835a9..0a9ba00 100644 Binary files a/license_plate.jpg and b/license_plate.jpg differ diff --git a/pico_files/main.py b/pico_files/main.py index 5077fdf..1b7238a 100644 --- a/pico_files/main.py +++ b/pico_files/main.py @@ -15,19 +15,23 @@ good_pin = Pin(15, Pin.OUT) bad_pin = Pin(14, Pin.OUT) # Send the POST request with the raw image data as the body -url = f'http://{config.server}:{config.port}/{config.data_path}' +url = f"http://{config.server}:{config.port}/{config.data_path}" -spi = SPI(0,sck=Pin(18), miso=Pin(16), mosi=Pin(19), baudrate=8000000) # Pins for the Raspberry Pi Pico +spi = SPI( + 0, sck=Pin(18), miso=Pin(16), mosi=Pin(19), baudrate=8000000 +) # Pins for the Raspberry Pi Pico cs = Pin(17, Pin.OUT) cam = Camera(spi, cs, debug_text_enabled=True) headers = {"Content-Type": "image/jpeg"} -addr = socket.getaddrinfo(config.server,config.port)[0][-1] +addr = socket.getaddrinfo(config.server, config.port)[0][-1] + + def send_image(image_file_path): cam.capture_jpg() - with open(image_file_path, 'wb') as f: + with open(image_file_path, "wb") as f: f.write(b"") cam.save_JPG(image_file_path) - with open(image_file_path, 'rb') as f: + with open(image_file_path, "rb") as f: s = socket.socket() s.connect(addr) # Send HTTP headers @@ -36,9 +40,9 @@ def send_image(image_file_path): s.send(b"Transfer-Encoding: chunked\r\n") s.send(b"Content-Type: application/octet-stream\r\n") s.send(b"\r\n") - + chunk_size = 512 - + while True: chunk = f.read(chunk_size) if not chunk: @@ -54,16 +58,17 @@ def send_image(image_file_path): # Final chunk to end the stream s.send(b"0\r\n\r\n") s.close() - + return + while connection.isconnected(): good_pin.low() bad_pin.low() wait_pin.high() - #if random.choice([True, False]): + # if random.choice([True, False]): # response = send_image("1234.jpg") - #else: + # else: # response = send_image("car2.jpg") response = send_image("image.jpg") print(response) @@ -73,4 +78,4 @@ while connection.isconnected(): good_pin.high() else: bad_pin.high() - sleep(5) + sleep(5) diff --git a/received_image.jpg b/received_image.jpg new file mode 100644 index 0000000..98f8fdb Binary files /dev/null and b/received_image.jpg differ