Reputation: 21
I want create a video steaming on socket, but I can't convert an image to bytearray. why i can't convert image form camera to bytearray?
CHUNK=1024 lnF = 640*480*3
wvs = WebcamVideoStream(0).start()
while True: for x in range(1):
try:
frame = wvs.read()
cv2_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# cv2.imshow('frame',cv2_im)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
frame = cv2.resize(frame, (640, 480))
# print("frame :", frame)
frame = np.array(frame, dtype = np.uint8).reshape(1, lnF)
# print("frame :",frame)
jpg = bytearray(frame)
# print("jpg_as_text", jpg_as_text)
# print("ACCEP")
except Exception as e:
print(e)
Upvotes: 2
Views: 1287
Reputation: 516
An cv2 image is stored as an numpy.ndarray. To get the byte representation of an numpy.ndarray you can just use the numpy.ndarray.tobytes method to convert you image. In your code it would look something like this:
jpg = frame.tobytes()
The length of the bytes when using the .tobytes method is actually a bit less when using is actually a bit smaller than the pickle.dumps method and a lot faster.
Upvotes: 1
Reputation: 28064
Try this:
import pickle
frame = wvs.read()
cv2_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.resize(frame, (640, 480))
frame = np.array(frame, dtype = np.uint8).reshape(1, lnF)
# packing:
pickled = pickle.dumps(frame)
byte_array_pickle = str.encode(pickled)
and
#unpacking
unicode_pickle = incoming_data.decode()
original_image = pickle.loads(unicode_pickle)
Upvotes: 0