current position:Home>How to write Medvedev's streaming in Python

How to write Medvedev's streaming in Python

2022-07-25 03:41:12CSDN Q & A

Bought the industrial camera of Medvedev , Now I don't know how to use python-opencv Write about the streaming of Medvedev , Official demo Can run out , Can call the camera , But if you use your own code, you can't call out the camera .
This is the official demo

import cv2
import numpy as np
import mvsdk
import time

class App(object):
def init(self):
super(App, self).init()
self.pFrameBuffer = 0
self.quit = False

def main(self):    #  Enumerate Cameras     DevList = mvsdk.CameraEnumerateDevice()    nDev = len(DevList)    if nDev < 1:        print("No camera was found!")        return    for i, DevInfo in enumerate(DevList):        print("{}: {} {}".format(i, DevInfo.GetFriendlyName(), DevInfo.GetPortType()))    i = 0 if nDev == 1 else int(input("Select camera: "))    DevInfo = DevList[i]    print(DevInfo)    #  Turn on camera     hCamera = 0    try:        hCamera = mvsdk.CameraInit(DevInfo, -1, -1)    except mvsdk.CameraException as e:        print("CameraInit Failed({}): {}".format(e.error_code, e.message) )        return    #  Get the camera property description     cap = mvsdk.CameraGetCapability(hCamera)    #  Judge whether it is a black-and-white camera or a color camera     monoCamera = (cap.sIspCapacity.bMonoSensor != 0)    #  Black and white camera let ISP Direct output MONO data , Instead of expanding into R=G=B Of 24 Bit gray     if monoCamera:        mvsdk.CameraSetIspOutFormat(hCamera, mvsdk.CAMERA_MEDIA_TYPE_MONO8)    else:        mvsdk.CameraSetIspOutFormat(hCamera, mvsdk.CAMERA_MEDIA_TYPE_BGR8)    #  Switch the camera mode to continuous acquisition     mvsdk.CameraSetTriggerMode(hCamera, 0)    #  Manual exposure , time of exposure 30ms    mvsdk.CameraSetAeState(hCamera, 0)    mvsdk.CameraSetExposureTime(hCamera, 30 * 1000)    #  Give Way SDK The internal drawing thread starts working     mvsdk.CameraPlay(hCamera)    #  Calculation RGB buffer Required size , Here, it is allocated directly according to the maximum resolution of the camera     FrameBufferSize = cap.sResolutionRange.iWidthMax * cap.sResolutionRange.iHeightMax * (1 if monoCamera else 3)    #  Distribute RGB buffer, For storage ISP Output image     #  remarks : Transfer from camera to PC The is RAW data , stay PC End through software ISP To RGB data ( If it is a black-and-white camera, there is no need to convert the format , however ISP There are other treatments , So you also need to allocate this buffer)    self.pFrameBuffer = mvsdk.CameraAlignMalloc(FrameBufferSize, 16)    #  Set the collection callback function     self.quit = False    mvsdk.CameraSetCallbackFunction(hCamera, self.GrabCallback, 0)    #  Waiting to exit     while not self.quit:        time.sleep(0.1)    #  Turn off camera     mvsdk.CameraUnInit(hCamera)    #  Free the frame cache     mvsdk.CameraAlignFree(self.pFrameBuffer)@mvsdk.method(mvsdk.CAMERA_SNAP_PROC)def GrabCallback(self, hCamera, pRawData, pFrameHead, pContext):    FrameHead = pFrameHead[0]    pFrameBuffer = self.pFrameBuffer    mvsdk.CameraImageProcess(hCamera, pRawData, pFrameBuffer, FrameHead)    mvsdk.CameraReleaseImageBuffer(hCamera, pRawData)        #  At this time, the picture has been stored in pFrameBuffer in , For color cameras pFrameBuffer=RGB data , Black and white camera pFrameBuffer=8 Bit grayscale data     #  hold pFrameBuffer convert to opencv Image format for subsequent algorithm processing     frame_data = (mvsdk.c_ubyte * FrameHead.uBytes).from_address(pFrameBuffer)    frame = np.frombuffer(frame_data, dtype=np.uint8)    frame = frame.reshape((FrameHead.iHeight, FrameHead.iWidth, 1 if FrameHead.uiMediaType == mvsdk.CAMERA_MEDIA_TYPE_MONO8 else 3) )    frame = cv2.resize(frame, (640,480), interpolation = cv2.INTER_LINEAR)    cv2.imshow("Press q to end", frame)    if (cv2.waitKey(1) & 0xFF) == ord('q'):        self.quit = True

def main():
app = App()


copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.

Random recommended