I have an ANPR HIKVision Parking Camera that detects vehicles and read their details such as Vehicle Registration Number, Vehicle Color, Vehicle Direction and Vehicle Type, and it also takes a snapshot of Vehicle Registration Number Plate. I am unable to decode the image which I get through ANPR Camera.
I have a code written that reads all of the information correctly. For the encoded image data which was coming from the live ANPR stream, I decided to store the data in a text file using the following code:
for line in response.iter_lines():
if line:
if 'Content-Type: image/jpeg' in str(line):
with open("vehicle_image.txt", "ab") as f:
print("Writing Image Text Line to File...")
f.write(line)
It successfully write all of the image data in a vehicle_image.txt file. And I wrote another script file to parse and decode the data. The code is below:
import re
from PIL import Image
from io import BytesIO
def extract_image_data(file_path):
with open(file_path, 'rb') as file:
binary_data = file.read()
# Find start and end markers
start_marker = b'Content-Length: 6215'
end_marker = b'--boundary'
start_index = binary_data.find(start_marker)
end_index = binary_data.find(end_marker)
if start_index == -1 or end_index == -1:
raise ValueError("Markers not found in the file")
# Extract image data
image_data = binary_data[start_index:end_index]
return image_data
def create_image_from_data(image_data):
# Create PIL Image object from image data
image = Image.open(BytesIO(image_data))
return image
if __name__ == "__main__":
file_path = "vehicle_image.txt"
try:
image_data = extract_image_data(file_path)
image = create_image_from_data(image_data)
# Save the image as a JPEG file
image.save("output.jpg")
print("Image saved as output.jpg")
except Exception as e:
print("An error occurred:", e)
Now when I run the above Script file, I get the following error:
An error occurred: cannot identify image file <_io.BytesIO object at 0x00000196112225E0>
Can anyone tell me what's wrong?