def __init__( self, videoPath, imageProcessingEndpoint = "", imageProcessingParams = "", showVideo = False, verbose = False, loopVideo = True, convertToGray = False, resizeWidth = 0, resizeHeight = 0, annotate = False, sendToHubCallback = None): self.videoPath = videoPath if self.__IsInt(videoPath): #case of a usb camera (usually mounted at /dev/video* where * is an int) self.isWebcam = True else: #case of a video file self.isWebcam = False self.imageProcessingEndpoint = imageProcessingEndpoint if imageProcessingParams == "": self.imageProcessingParams = "" else: self.imageProcessingParams = json.loads(imageProcessingParams) self.showVideo = showVideo self.verbose = verbose self.loopVideo = loopVideo self.convertToGray = convertToGray self.resizeWidth = resizeWidth self.resizeHeight = resizeHeight self.annotate = (self.imageProcessingEndpoint != "") and self.showVideo & annotate self.nbOfPreprocessingSteps = 0 self.autoRotate = False self.sendToHubCallback = sendToHubCallback self.vs = None if self.convertToGray: self.nbOfPreprocessingSteps +=1 if self.resizeWidth != 0 or self.resizeHeight != 0: self.nbOfPreprocessingSteps +=1 if self.verbose: print("Initialising the camera capture with the following parameters: ") print(" - Video path: " + self.videoPath) print(" - Image processing endpoint: " + self.imageProcessingEndpoint) print(" - Image processing params: " + json.dumps(self.imageProcessingParams)) print(" - Show video: " + str(self.showVideo)) print(" - Loop video: " + str(self.loopVideo)) print(" - Convert to gray: " + str(self.convertToGray)) print(" - Resize width: " + str(self.resizeWidth)) print(" - Resize height: " + str(self.resizeHeight)) print(" - Annotate: " + str(self.annotate)) print(" - Send processing results to hub: " + str(self.sendToHubCallback is not None)) print() self.displayFrame = None if self.showVideo: self.imageServer = ImageServer(5012, self) self.imageServer.start()
def __init__(self, videoPath="", verbose=True, videoW=0, videoH=0, fontScale=1.0, inference=True, confidenceLevel=0.5, detectionSampleRate=10, imageProcessingEndpoint=""): self.videoPath = videoPath self.verbose = verbose self.videoW = videoW self.videoH = videoH self.inference = inference self.confidenceLevel = confidenceLevel self.useStream = False self.useStreamHttp = False self.useMovieFile = False self.frameCount = 0 self.vStream = None self.vCapture = None self.displayFrame = None self.fontScale = float(fontScale) self.captureInProgress = False self.imageResp = None self.url = "" self.detectionSampleRate = detectionSampleRate self.imageProcessingEndpoint = imageProcessingEndpoint print("VideoCapture::__init__()") print("OpenCV Version : %s" % (cv2.__version__)) print( "===============================================================") print("Initialising Video Capture with the following parameters: ") print(" - Video path : " + self.videoPath) print(" - Video width : " + str(self.videoW)) print(" - Video height : " + str(self.videoH)) print(" - Font Scale : " + str(self.fontScale)) print(" - Inference? : " + str(self.inference)) print(" - ConficenceLevel : " + str(self.confidenceLevel)) print(" - Dct smpl rate : " + str(self.detectionSampleRate)) print(" - Imageproc.Endpt.: " + str(self.imageProcessingEndpoint)) print("") self.imageServer = ImageServer(80, self) self.imageServer.start() self.yoloInference = YoloInference(self.fontScale)
def __init__(self, videoPath, imageProcessingEndpoint="", imageProcessingParams="", showVideo=False, verbose=False, resizeWidth=0, resizeHeight=0, sendToHubCallback=None): self.videoPath = videoPath self.imageProcessingEndpoint = imageProcessingEndpoint if imageProcessingParams == "": self.imageProcessingParams = "" else: self.imageProcessingParams = json.loads(imageProcessingParams) self.showVideo = showVideo self.verbose = verbose self.resizeWidth = resizeWidth self.resizeHeight = resizeHeight self.nbOfPreprocessingSteps = 0 self.autoRotate = False self.sendToHubCallback = sendToHubCallback self.vs = None if self.resizeWidth != 0 or self.resizeHeight != 0: self.nbOfPreprocessingSteps += 1 if self.verbose: print( "Initialising the camera capture with the following parameters: " ) print(" - Video path: ", self.videoPath) print(" - Image processing endpoint: " + self.imageProcessingEndpoint) print(" - Image processing params: " + json.dumps(self.imageProcessingParams)) print(" - Show video: " + str(self.showVideo)) print(" - Resize width: " + str(self.resizeWidth)) print(" - Resize height: " + str(self.resizeHeight)) print(" - Send processing results to hub: " + str(self.sendToHubCallback is not None)) print() self.displayFrame = None if self.showVideo: self.imageServer = ImageServer(5012, self) self.imageServer.start()
def __init__(self, videoPath, imageProcessingEndpoint="", imageProcessingParams="", showVideo=False, verbose=False, loopVideo=True, convertToGray=False, resizeWidth=0, resizeHeight=0, annotate=False, sendToHubCallback=None, delay=0, computerVisionEndpoint_FULL=None, computerVisionKey=None): self.delay = delay self.computerVisionEndpoint_FULL = computerVisionEndpoint_FULL self.computerVisionKey = computerVisionKey self.videoPath = videoPath if self.__IsInt(videoPath): #case of a usb camera (usually mounted at /dev/video* where * is an int) self.isWebcam = True else: #case of a video file self.isWebcam = False self.imageProcessingEndpoint = imageProcessingEndpoint if imageProcessingParams == "": self.imageProcessingParams = "" else: # self.imageProcessingParams = json.loads(imageProcessingParams) self.imageProcessingParams = json.loads( '{"visualFeatures":"tags"}') self.showVideo = showVideo self.verbose = verbose self.loopVideo = loopVideo self.convertToGray = convertToGray self.resizeWidth = resizeWidth self.resizeHeight = resizeHeight self.annotate = (self.imageProcessingEndpoint != "") and self.showVideo & annotate self.nbOfPreprocessingSteps = 0 self.autoRotate = False self.sendToHubCallback = sendToHubCallback self.vs = None if self.convertToGray: self.nbOfPreprocessingSteps += 1 if self.resizeWidth != 0 or self.resizeHeight != 0: self.nbOfPreprocessingSteps += 1 if self.verbose: print( "Initialising the camera capture with the following parameters: " ) print(" - Video path: " + self.videoPath) print(" - Image processing endpoint: " + self.imageProcessingEndpoint) print(" - Image processing params: " + json.dumps(self.imageProcessingParams)) print(" - Show video: " + str(self.showVideo)) print(" - Loop video: " + str(self.loopVideo)) print(" - Convert to gray: " + str(self.convertToGray)) print(" - Resize width: " + str(self.resizeWidth)) print(" - Resize height: " + str(self.resizeHeight)) print(" - Annotate: " + str(self.annotate)) print(" - Send processing results to hub: " + str(self.sendToHubCallback is not None)) print() self.displayFrame = None if self.showVideo: self.imageServer = ImageServer(5012, self) self.imageServer.start() if self.computerVisionEndpoint_FULL: # self.credentials = CognitiveServicesCredentials(self.computerVisionKey) # self.client = ComputerVisionClient(self.computerVisionEndpoint_FULL, self.credentials) self.computerVisionHeader = { 'Ocp-Apim-Subscription-Key': self.computerVisionKey, 'Content-Type': 'application/octet-stream' }
def __init__(self, videoPath, imageProcessingEndpoint="", imageProcessingParams="", imageStorageEndpoint="", storeImage=False, showVideo=False, verbose=False, loopVideo=True, convertToGray=False, resizeWidth=0, resizeHeight=0, annotate=False, sendToHubCallback=None): self.videoPath = videoPath if self.__IsInt(videoPath): #case of a usb camera (usually mounted at /dev/video* where * is an int) self.isWebcam = True else: #case of a video file self.isWebcam = False self.imageProcessingEndpoint = imageProcessingEndpoint if imageProcessingParams == "": self.imageProcessingParams = "" else: self.imageProcessingParams = json.loads(imageProcessingParams) if imageStorageEndpoint == "": self.imageStorageEndpoint = "" else: self.imageStorageEndpoint = json.loads(imageStorageEndpoint) self.storeImage = storeImage self.showVideo = showVideo self.verbose = verbose self.loopVideo = loopVideo self.convertToGray = convertToGray self.resizeWidth = resizeWidth self.resizeHeight = resizeHeight self.annotate = (self.imageProcessingEndpoint != "") and self.showVideo & annotate self.nbOfPreprocessingSteps = 0 self.autoRotate = False self.sendToHubCallback = sendToHubCallback self.vs = None if self.convertToGray: self.nbOfPreprocessingSteps += 1 if self.resizeWidth != 0 or self.resizeHeight != 0: self.nbOfPreprocessingSteps += 1 if self.verbose: print( "Initialising the camera capture with the following parameters: " ) print(" - Video path: " + self.videoPath) print(" - Image processing endpoint: " + self.imageProcessingEndpoint) print(" - Image processing params: " + json.dumps(self.imageProcessingParams)) print(" - Image storage endpoint: " + json.dumps(self.imageStorageEndpoint)) print(" - Store image: " + str(self.storeImage)) print(" - Show video: " + str(self.showVideo)) print(" - Loop video: " + str(self.loopVideo)) print(" - Convert to gray: " + str(self.convertToGray)) print(" - Resize width: " + str(self.resizeWidth)) print(" - Resize height: " + str(self.resizeHeight)) print(" - Annotate: " + str(self.annotate)) print(" - Send processing results to hub: " + str(self.sendToHubCallback is not None)) print() self.displayFrame = None if self.showVideo: self.imageServer = ImageServer(5012, self) self.imageServer.start() if self.storeImage: try: # Create the BlockBlockService that is used to call the Blob service for the storage account self.block_blob_service = BlockBlobService( account_name=self.imageStorageEndpoint["accountname"], account_key=self.imageStorageEndpoint["accountkey"]) # Create a container self.block_blob_service.create_container( self.imageStorageEndpoint["containername"]) except Exception as e: print("Failed to set up blob container: " + str(e)) # Allow container to continue but override storeImage self.storeImage = False
def main(imageProcessingEndpoint="", imageProcessingParams="", showVideo=False, verbose=False, loopVideo=True, convertToGray=False, resizeWidth=0, resizeHeight=0, annotate=False): ''' Capture a camera feed, send it to processing and forward outputs to EdgeHub :param str imageProcessingEndpoint: service endpoint to send the frames to for processing. Example: "http://face-detect-service:8080". Leave empty when no external processing is needed (Default). Optional. :param str imageProcessingParams: query parameters to send to the processing service. Example: "'returnLabels': 'true'". Empty by default. Optional. :param bool showVideo: show the video in a windows. False by default. Optional. :param bool verbose: show detailed logs and perf timers. False by default. Optional. :param bool loopVideo: when reading from a video file, it will loop this video. True by default. Optional. :param bool convertToGray: convert to gray before sending to external service for processing. False by default. Optional. :param int resizeWidth: resize frame width before sending to external service for processing. Does not resize by default (0). Optional. :param int resizeHeight: resize frame width before sending to external service for processing. Does not resize by default (0). Optional.ion( :param bool annotate: when showing the video in a window, it will annotate the frames with rectangles given by the image processing service. False by default. Optional. Rectangles should be passed in a json blob with a key containing the string rectangle, and a top left corner + bottom right corner or top left corner with width and height. ''' try: print("\nPython %s\n" % sys.version) print("Camera Capture Azure IoT Edge Module. Press Ctrl-C to exit.") try: global hubManager global twin_flag hubManager = HubManager(10000, IoTHubTransportProvider.MQTT, verbose) print("waiting for twin props") while twin_flag: time.sleep(1) pass except IoTHubError as iothub_error: print("Unexpected error %s from IoTHub" % iothub_error) return global camera_capture global imageServer while True: with CameraCapture(video_path, imageProcessingEndpoint, imageProcessingParams, showVideo, verbose, loopVideo, convertToGray, resizeWidth, resizeHeight, annotate, send_to_Hub_callback) as cameraCapture: camera_capture = cameraCapture if showVideo: if imageServer == None: imageServer = ImageServer(5012, cameraCapture) imageServer.start() else: imageServer.setCameraCapture(cameraCapture) cameraCapture.start() except KeyboardInterrupt: print("Camera capture module stopped")