meekworth / pylwdrone

Python module to communicate with a lewei camera module.
Apache License 2.0
19 stars 4 forks source link

using the feed in opencv #10

Open csaail opened 7 months ago

csaail commented 7 months ago

how can i use this feed in opencv for image processing

nobu835 commented 20 hours ago

you can use h264decoder: https://github.com/DaWelter/h264decoder

nobu835 commented 13 hours ago

This is the simplest example. Combined the h264decoder sample and the pylwdrone sample.

import cv2 # you can use opencv!

# pylwdrone setup
import pylwdrone
drone = pylwdrone.LWDrone()

# based on the "H264 Decoder Python Module" Example (copy and pasted)
import h264decoder
import numpy as np

#f = open(thefile, 'rb')
decoder = h264decoder.H264Decoder()
while 1:
#  data_in = f.read(1024)
#  if not data_in:
#    break
#  framedatas = decoder.decode(data_in)

  for _frame in drone.start_video_stream(): # pylwdrone "Stream live video" Example
    framedatas=decoder.decode(bytes(_frame.frame_bytes))
    # Note that the variable name "frame" is the same in both pyledrone and h264decoder.
    # Here, we change the "frame" of pylwdrone to "_frame".
    # `type(_frame.frame_bytes)`  is `<class 'bytearray'>`.
    # `decoder.decode()`  takes <class 'bytes'> as argument.
    # Here we explicitly convert it to 'bytes' type.

    # Let's go back to the "H264 Decoder Python Module" Example.
    for framedata in framedatas:
      (frame, w, h, ls) = framedata
      if frame is not None:
          #print('frame size %i bytes, w %i, h %i, linesize %i' % (len(frame), w, h, ls))
          frame = np.frombuffer(frame, dtype=np.ubyte, count=len(frame))
          frame = frame.reshape((h, ls//3, 3))
          frame = frame[:,:w,:]
          # End of "H264 Decoder Python Module" Example

          frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB) # cv2 uses BGR
          cv2.imshow("window",frame)
          cv2.waitKey(1)

By using the threading module, you can do something else (such as piloting a drone) while watching the video. If you want a sample of this, please let me know. I plan to try making it from now on too.