I am connected to a HikVision camera stream and I am able to isolate picture data but I am not able to convert that data into an image. If I save the binary data to an image file, the file doesn't show any image. If I use Pillow , it says no image identified.
Here is the image data : https://drive.google.com/file/d/1z1TfPb88Ng5qkXWZZ9QfrZR4H5fh43pg/view?usp=sharing ..
The text is too long for stackoverflow or pastebin..
This is how I am getting the data from the stream:
with requests.get(url, auth=auth, headers=headers, stream=True) as response:
response_text = ''
license_plate = ''
direction = ''
color = ''
is_picture = False
picture_data = b''
for line in response.iter_lines():
if not line:
continue
line_str = str(line)
if 'Content-Type: image/jpeg' in line_str:
is_picture = True
picture_data = b''
elif is_picture:
picture_data += line
response_text += line.decode('utf-8', errors='ignore')
if '--boundary' not in response_text:
continue
if is_picture:
with open('picture_data.txt', 'wb') as f:
f.write(picture_data)
print(picture_data)
print('picture saved')
is_picture = False
# picture_data = b''
response_text = response_text.rstrip('--boundary')
split_text = response_text.split('<EventNotificationAlert')
if len(split_text) > 1:
notification_alert = '<EventNotificationAlert' + split_text[1]
event_type = '<eventType></eventType>'
event_type = re.search('<eventType>(.*?)</eventType>', notification_alert).group(1)
if event_type == 'ANPR':
with open('test.xml', 'a') as f:
f.writelines(notification_alert)
else:
print('not anpr')
else:
print("'<EventNotificationAlert' not found in response_text")
response_text = ''
And this is how I am trying to decode the picture_data:
bytes = picture_data
a = bytes.find(b'\xff\xd8')
b = bytes.find(b'\xff\xd9')
if a != -1 and b != -1:
jpg = bytes[a:b+8]
bytes = bytes[b+2:]
i = Image.open(BytesIO(jpg))
buffered = BytesIO()
i.save(buffered, format="JPEG")
byte_im = buffered.getvalue()
This does not work, the PIL library is not able to identify any image.
I am able to get an image from the camera directly using:
def get_picture_direct(self, registration):
url = f'http://{self.stream_url}/ISAPI/Streaming/channels/101/picture'
auth = HTTPDigestAuth(self.stream_username, self.stream_password)
response = requests.get(url, auth=auth)
jpg = response.content
i = Image.open(BytesIO(jpg))
buffered = BytesIO()
i.save(buffered, format="JPEG")
byte_im = buffered.getvalue()
url=self.upload_image_to_firestore(vehicle_registration=registration, bucket_name=f'{self.storage_bucket}', data=byte_im)
return url
The event stream from the camera send ANPR event notifications and an image comes with each ANPR notification. If I use the direct method after the notification, the image is captured too late and misses the vehicle number plate.
How do convert tyhis byte data to an image correctly?
Any help is greatly appreciated.
this is the sample code for receive the anpr alarm from Hikvision anpr camera in listening mode and save the plate number and plate number image by python: