Skip to content

Instantly share code, notes, and snippets.

@rayepeng
Created June 8, 2021 02:16
Show Gist options
  • Save rayepeng/4b35531fa9a93f1f09993d7ecc4bfeb1 to your computer and use it in GitHub Desktop.
Save rayepeng/4b35531fa9a93f1f09993d7ecc4bfeb1 to your computer and use it in GitHub Desktop.
海康相机多线程
def Work_thread(self,root,panel):
stOutFrame = MV_FRAME_OUT()
img_buff = None
buf_cache = None
numArray = None
while True:
ret = self.obj_cam.MV_CC_GetImageBuffer(stOutFrame, 1000)
if 0 == ret:
if None == buf_cache:
buf_cache = (c_ubyte * stOutFrame.stFrameInfo.nFrameLen)()
#获取到图像的时间开始节点获取到图像的时间开始节点
self.st_frame_info = stOutFrame.stFrameInfo
cdll.msvcrt.memcpy(byref(buf_cache), stOutFrame.pBufAddr, self.st_frame_info.nFrameLen)
print ("get one frame: Width[%d], Height[%d], nFrameNum[%d]" % (self.st_frame_info.nWidth, self.st_frame_info.nHeight, self.st_frame_info.nFrameNum))
self.n_save_image_size = self.st_frame_info.nWidth * self.st_frame_info.nHeight * 3 + 2048
if img_buff is None:
img_buff = (c_ubyte * self.n_save_image_size)()
if True == self.b_save_jpg:
self.Save_jpg(buf_cache) #ch:保存Jpg图片 | en:Save Jpg
if True == self.b_save_bmp:
self.Save_Bmp(buf_cache) #ch:保存Bmp图片 | en:Save Bmp
else:
print("no data, nret = "+self.To_hex_str(ret))
continue
#转换像素结构体赋值
stConvertParam = MV_CC_PIXEL_CONVERT_PARAM()
memset(byref(stConvertParam), 0, sizeof(stConvertParam))
stConvertParam.nWidth = self.st_frame_info.nWidth
stConvertParam.nHeight = self.st_frame_info.nHeight
stConvertParam.pSrcData = cast(buf_cache, POINTER(c_ubyte))
stConvertParam.nSrcDataLen = self.st_frame_info.nFrameLen
stConvertParam.enSrcPixelType = self.st_frame_info.enPixelType
# RGB直接显示
if PixelType_Gvsp_RGB8_Packed == self.st_frame_info.enPixelType:
numArray = CameraOperation.Color_numpy(self,buf_cache,self.st_frame_info.nWidth,self.st_frame_info.nHeight)
#如果是彩色且非RGB则转为RGB后显示
else:
nConvertSize = self.st_frame_info.nWidth * self.st_frame_info.nHeight * 3
stConvertParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed
stConvertParam.pDstBuffer = (c_ubyte * nConvertSize)()
stConvertParam.nDstBufferSize = nConvertSize
time_start=time.time()
ret = self.obj_cam.MV_CC_ConvertPixelType(stConvertParam)
time_end=time.time()
print('MV_CC_ConvertPixelType:',time_end - time_start)
if ret != 0:
tkinter.messagebox.showerror('show error','convert pixel fail! ret = '+self.To_hex_str(ret))
continue
cdll.msvcrt.memcpy(byref(img_buff), stConvertParam.pDstBuffer, nConvertSize)
numArray = CameraOperation.Color_numpy(self,img_buff,self.st_frame_info.nWidth,self.st_frame_info.nHeight)
#合并OpenCV到Tkinter界面中
current_image = Image.fromarray(numArray).resize((800, 600), Image.ANTIALIAS)
imgtk = ImageTk.PhotoImage(image=current_image, master=root)
panel.imgtk = imgtk
panel.config(image=imgtk)
root.obr = imgtk
nRet = self.obj_cam.MV_CC_FreeImageBuffer(stOutFrame)
if self.b_exit == True:
if img_buff is not None:
del img_buff
if buf_cache is not None:
del buf_cache
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment