-1

my problem is the camera so slow and not smooth and that makes me a problem to realtime extract text from the label. I want to know how to make a preview of the camera more smooth like a video camera, there is a big lag between live and stream? here is my code

index = 0
#while test_vid.isOpened():
#make_480p()
while True:
    frames += 1
    test_vid = cv2.VideoCapture(0)
    test_vid.set(cv2.CAP_PROP_FRAME_WIDTH, 720)
    test_vid.set(cv2.CAP_PROP_FRAME_HEIGHT, 640)
    test_vid.set(cv2.CAP_PROP_FPS,1)
    fps = test_vid.get(cv2.CAP_PROP_FPS)
    print ("Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}".format(fps))
    ret,frame = test_vid.read()
    start = time.time()
    ret, frame = test_vid.read()
    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    end = time.time()
    print ("time to read a frame : {} seconds".format(end-start))
    print(frame)
    frame = cv2.flip(frame, 10, 0)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow("LIVE", gray)
    key = cv2.waitKey(1)
    if key == ord('q'):
        break
    name = './image_frames/frame' + str(index) + '.png'
    print ('Extracting frames...' + name)
    cv2.imshow('frame',frame)
    cv2.imwrite(name, frame)

test_vid.release()
cv2.destroyAllWindows()  
demo = Image.open("./image_frames/frame0.png")
text = pytesseract.image_to_string(demo, lang = 'eng')
print(text) 
CDJB
  • 14,043
  • 5
  • 29
  • 55
  • Have you done any profiling or benchmarking? You haven't even provided all the necessary code, or even some explanations of your program. – AMC Feb 10 '20 at 01:38

1 Answers1

1

One issue is that you're recreating the VideoCapture object every frame. Move that setup code outside the loop and you should see a speedup.

# setup the video capture device
test_vid = cv2.VideoCapture(0)
test_vid.set(cv2.CAP_PROP_FRAME_WIDTH, 720)
test_vid.set(cv2.CAP_PROP_FRAME_HEIGHT, 640)
test_vid.set(cv2.CAP_PROP_FPS,1)
fps = test_vid.get(cv2.CAP_PROP_FPS)
print ("Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}".format(fps))

while test_vid.isOpened():
    ret,frame = test_vid.read()
    # do frame processing...

    # calculate wait time based on the camera fps
    # (as an extension, measure and subtract the image processing time)
    key = cv2.waitKey(int(1000/fps))
    if key == ord('q'):
        break
Dominic D
  • 1,778
  • 2
  • 5
  • 12
  • Made a few improvements on your answer. Sorry about that. Plus `cv2.waitKey(1)` should actually be `cv2.waitKey(int(1000/fps))`. – karlphillip Feb 09 '20 at 21:51
  • i have same problem of camera speed like in this link ,https://stackoverflow.com/questions/58293187/opencv-real-time-streaming-video-capture-is-slow-how-to-drop-frames-or-get-sync ,i am work local in laptop camera not ip camera and i want to speed my camera frame with good frame to can extract text from frame. – Mina Alexander Feb 09 '20 at 22:07
  • Does your video output seem constantly slow? Or does it seem that the a delay builds up over time? Can you run minimal example code (without the image processing, just capturing and showing the frame)? I think the stream will also appear slow when you've set it to 1 FPS though. – Dominic D Feb 09 '20 at 22:16
  • thanks, Dominic but still there is a lag in my stream from the cam and can not extract all text from the label – Mina Alexander Feb 09 '20 at 22:21
  • I test using 1 fps and using 30 the same lag? – Mina Alexander Feb 09 '20 at 22:26
  • Are you chaging the waitKey time ```key = cv2.waitKey(int(1000/fps))```? – Dominic D Feb 09 '20 at 22:27
  • when I connect my mobile and use as cam works smooth but when running code on mobile cam there is a big lag – Mina Alexander Feb 09 '20 at 22:38