Opencv cannot run as usual #2321
Answered
by
Buruxianian
Buruxianian
asked this question in
Q&A
-
QuestionUnable to obtain frame by running nicegui-main\examples\opencv_webcam\main.py To solve the problem, I compared two simplified scripts. But I don't know how to solve it. import base64
import cv2
from nicegui import ui
video_capture = cv2.VideoCapture(0)
ui_interactive_image = ui.interactive_image()
def lazy_update() -> None:
global video_capture, ui_interactive_image
ret, frame = video_capture.read()
if ret and ui_interactive_image is not None:
_, imencode_image = cv2.imencode('.jpg', frame)
base64_image = base64.b64encode(imencode_image)
base64_image_string = 'data:image/jpg;base64,' + base64_image.decode('ascii')
ui_interactive_image.source = base64_image_string
ui.timer(interval=2, callback=lazy_update)
ui.run() The second script can retrieve frames, as detailed below: import base64
import cv2
from nicegui import ui
ui_interactive_image = ui.interactive_image()
def lazy_update() -> None:
global ui_interactive_image
video_capture = cv2.VideoCapture(0)
ret, frame = video_capture.read()
if ret and ui_interactive_image is not None:
_, imencode_image = cv2.imencode('.jpg', frame)
base64_image = base64.b64encode(imencode_image)
base64_image_string = 'data:image/jpg;base64,' + base64_image.decode('ascii')
ui_interactive_image.source = base64_image_string
video_capture.release()
ui.timer(interval=2, callback=lazy_update)
ui.run() |
Beta Was this translation helpful? Give feedback.
Answered by
Buruxianian
Jan 9, 2024
Replies: 3 comments 10 replies
-
Have you seen and tried our OpenCV webcam example? |
Beta Was this translation helpful? Give feedback.
1 reply
-
The problem has been resolved.
After turning off the reload, it will run normally |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Buruxianian
-
So is this a bug? or should we update the OpenCV exmple ? |
Beta Was this translation helpful? Give feedback.
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem has been resolved.
After turning off the reload, it will run normally