目前,我正在通過使用以下代碼來讀取IP攝像機的實時影像:
def livestream(self)。
print("start"/span>)
stream = urlopen('http://192.168.4.1:81/stream')
bytes = b''
while True:
try:
bytes = stream.read(1024)
a = bytes.find(b'xffxd8')
b = bytes.find(b'xffxd9')
if a != -1 and b != -1。
jpg = bytes[a:b 2]
bytes = bytes[b 2: ]
getliveimage = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
livestreamrotated1 = cv2.rotate(getliveimage, cv2.ROTATE_90_CLOCKWISE) #這里我在旋轉圖片。
print(type(livestreamrotated1) #type at this point is <class 'numpy.ndarray' >
cv2.imshow('video', livestreamrotated1)
if cv2.waitKey(1) ==27: # if user hit esc: 如果用戶點擊了esc.
exit(0) # exit program
except Exception as e:
print(e)
print(" failed at this point"/span>)
現在我想把結果影像集成到Kivy-GUI中,并想擺脫while-loop,因為它凍結了我的GUI。不幸的是,這個回圈對于逐個重新創建影像是必要的。我想用cv2.VideoCapture來代替,并且每秒安排多次。這根本行不通,我無法通過這種方式從直播流中捕獲影像......我錯在哪里?
cap = cv2.VideoCapture('http://192.168.4.1:81/stream?dummy.jpg')
ret, frame = cap.read()
cv2.imshow('stream', frame)
我在其他一些帖子中讀到,在這一點上,像 "dummy.jpg "這樣的檔案結尾是必要的,但它仍然不作業,程式凍結。
。請幫助。請提前感謝!
uj5u.com熱心網友回復:
如果你想把你的閱讀回圈和你的GUI回圈解耦,你可以使用多執行緒來分離這些代碼。你可以讓一個執行緒運行你的livestream函式,并將影像轉儲到一個全域影像變數中,你的GUI回圈可以接收它并對它進行任何操作。
我無法真正測驗代碼中的直播部分,但類似這樣的代碼應該可以作業。讀取函式是一個例子,說明如何撰寫一個通用的回圈函式,它將與這段代碼一起作業。
import cv2
import time
import執行緒
import numpy as np
# 通用執行緒類
class Reader(threading.Thread)。
def __init__(self, func, *args)。
threading.Thread.__init__(self, target = func, args = args)。
self.start()。
# globals用于管理共享資料。
g_stop_threads = False。
g_lock = threading.Lock();
g_frame = None;
# 從vidcap中讀取幀并存盤在g_frame中。
def read()。
# grab globals
global g_stop_threads。
global g_lock;
global g_frame;
# open vidcap
cap = cv2.VideoCapture(0)。
# loop
while not g_stop_threads:
# get a frame from camera[/span]。
ret, frame = cap.read();
# 替換全域幀 # 替換全域幀
if ret:
with g_lock:
# 復制,這樣我們就可以快速丟棄鎖。
g_frame = np.copy(frame)。
# sleep so that someone else can use the lock[/span]。
time.sleep(0.03); #以秒為單位。
# your livestream func.
def livestream()。
# grab globals livestream().
global g_stop_threads。
global g_lock;
global g_frame;
# open stream
stream = urlopen('http://192.168.4.1:81/stream')
bytes = b''
# process stream into opencv image[/span]。
while not g_stop_threads:
try:
bytes = stream.read(1024)
a = bytes.find(b'xffxd8')
b = bytes.find(b'xffxd9')
if a != -1 and b != -1。
jpg = bytes[a:b 2]
bytes = bytes[b 2: ]
getliveimage = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
livestreamrotated1 = cv2.rotate(getliveimage, cv2.ROTATE_90_CLOCKWISE) #這里我在旋轉圖片。
#獲取鎖定并替換影像。
with g_lock:
g_frame = livestreamrotated1;
# sleep to allow other threads to get the lock0.03); # 以秒為單位。
except Exception as e:
print(e)
print(" failed at this point"/span>)
def main()。
# grab globals main().
global g_stop_threads。
global g_lock;
global g_frame;
# 啟動一個執行緒。
# reader = Reader(read);
reader = Reader(livestream);
# 顯示g_frame的幀
my_frame = None;
while True:
# grab lock; while.
with g_lock:
# show with g_lock.
if not g_frame is None:
# copy # 我們在這里復制,以盡可能快地轉儲鎖。
my_frame = np.copy(g_frame);
# 現在我們可以在沒有鎖的情況下做所有緩慢的操作/gui的事情。
if my_frame is not None:
cv2.imshow("Frame", my_frame);
# break out if 'q' is pressed.
if cv2.waitKey(1) == ord('q'/span>) 。
break。
#停止執行緒。
g_stop_threads = True;
if __name__ == "__main__"/span>:
main()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/328636.html
標籤:
上一篇:在谷歌地圖上的多個標記之間畫線
