我正在使用 Basler 相機開發 4 個 GUI。在我的程式中,我面臨兩個導致 GUI 無法正常運行的問題。但是,主要目標是根據相機索引保存 4 張影像。我被告知使用多執行緒技術來開發GUI,這些是我遇到的問題。這些是代碼:
def LiveThread(strIdx):
CamIdx = int(strIdx)
try:
panel[CamIdx] = None
image[CamIdx] = []
# Start Grabbing
camera[CamIdx].StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
print("Cam",CamIdx,': Start Grabbing')
iterator = 0
while bLiveThraed[CamIdx]:
grabResult = camera[CamIdx].RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
if grabResult.GrabSucceeded():
image[CamIdx] = converter[CamIdx].Convert(grabResult) # Access the openCV image data
image[CamIdx] = image[CamIdx].GetArray() # change them to an array for easy access
if(image[CamIdx] != []):
image[CamIdx] = cv2.cvtColor(image[CamIdx], cv2.COLOR_BGR2RGB)
image[CamIdx] = cv2.resize(image[CamIdx], (345,270))
image[CamIdx] = ImageTk.PhotoImage(image = Image.fromarray(image[CamIdx]))
if panel[CamIdx] is None:
panel[CamIdx] = tk.Label(image=image[CamIdx])
panel[CamIdx].image = image[CamIdx]
panel[CamIdx].pack(side="left")
panel[CamIdx].place(x=(345*CamIdx) (20*CamIdx) 20, y=100)
panel[CamIdx] = tk.Label(image=image[CamIdx])
panel[CamIdx].image = image[CamIdx]
panel[CamIdx].pack(side="bottom")
panel[CamIdx].place(x=(345*CamIdx) (20*CamIdx) 20, y=400)
#cv2.imwrite('./trial/camera' str(CamIdx) str(iterator) '.jpg', image[CamIdx])
#iterator =1
else:
panel[CamIdx].configure(image=image[CamIdx])
panel[CamIdx].image = image[CamIdx]
else:
print("Error: ", grabResult.ErrorCode)
grabResult.Release()
except genicam.GenericException as e:
# Error handling
print("An exception occurred.", e.GetDescription())
- 不推薦使用的警告顯示在下圖中

- 執行
cv2.imwrite('./trial/camera' str(CamIdx) str(iterator) '.jpg', image[CamIdx]). 下圖給出了由此產生的錯誤。

生成的 GUI 如下圖所示。

uj5u.com熱心網友回復:
在下面的代碼中,最后一行image[CamIdx]從 opencv 影像分配成為ImageTk.PhotoImagecv2 無法記下的。考慮image[CamIdx]在分配之前存盤到另一個變數image[CamIdx] = ImageTk.PhotoImage(image = Image.fromarray(image[CamIdx]))。
if(image[CamIdx] != []):
image[CamIdx] = cv2.cvtColor(image[CamIdx], cv2.COLOR_BGR2RGB)
image[CamIdx] = cv2.resize(image[CamIdx], (345,270))
image[CamIdx] = ImageTk.PhotoImage(image = Image.fromarray(image[CamIdx]))
到
save_img = None
if(image[CamIdx] != []):
image[CamIdx] = cv2.cvtColor(image[CamIdx], cv2.COLOR_BGR2RGB)
image[CamIdx] = cv2.resize(image[CamIdx], (345,270))
save_img = image[CamIdx]
image[CamIdx] = ImageTk.PhotoImage(image = Image.fromarray(image[CamIdx]))
cv2.imwrite('img.jpg',save_img)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/367619.html
下一篇:例外值:加載libasound.so.2失敗:libasound.so.2:無法打開共享物件檔案:沒有這樣的檔案或目錄(Django/heroku)
