示例#1
0
文件: camera.py 项目: vossenv/spypi
 def stop(self):
     self.logger.info("Stopping arducam")
     self.running = False
     ArducamSDK.Py_ArduCam_del(self.handle)
     ArducamSDK.Py_ArduCam_flush(self.handle)
     ArducamSDK.Py_ArduCam_endCaptureImage(self.handle)
     ArducamSDK.Py_ArduCam_close(self.handle)
     self.images.clear()
     self.logger.info("Arducam stopped")
示例#2
0
def readImage_thread(publisher_img):
    global handle, running, Width, Height, save_flag, cfg, color_mode, rt_lock, count1
    global COLOR_BayerGB2BGR, COLOR_BayerRG2BGR, COLOR_BayerGR2BGR, COLOR_BayerBG2BGR
    global logpath, loghandle, log_stepsize, logging, curr_logging, loghandle1, max_num_frames_log, curr_num_frames_log

    bridge = CvBridge()

    count = 0
    totalFrame = 0
    time0 = time.time()
    time1 = time.time()
    data = {}

    # DEMO WINDOW DECISION!!!
    cv2.namedWindow("ArduCam Demo", 1)

    if not os.path.exists("images1"):
        os.makedirs("images1")
        print "CIAO"
    if not os.path.exists(logpath):
        oldmask = os.umask(000)
        os.makedirs(logpath, 0777)
        print "CREATING DIRECTORY"

    while running:
        if count1 == 1:
            if rt_lock.acquire(False):
                print "read image", count1
                display_time = time.time()
                if ArducamSDK.Py_ArduCam_availableImage(handle) > 0:
                    rtn_val, data, rtn_cfg = ArducamSDK.Py_ArduCam_readImage(
                        handle)
                    datasize = rtn_cfg['u32Size']
                    if rtn_val != 0:
                        print "read data fail!"
                        continue

                    if datasize == 0:
                        continue

                    image = None
                    emImageFmtMode = cfg['emImageFmtMode']
                    if emImageFmtMode == ArducamSDK.FORMAT_MODE_JPG:
                        image = JPGToMat(data, datasize)
                    if emImageFmtMode == ArducamSDK.FORMAT_MODE_YUV:
                        image = YUVToMat(data)
                    if emImageFmtMode == ArducamSDK.FORMAT_MODE_RGB:
                        image = RGB565ToMat(data)
                    if emImageFmtMode == ArducamSDK.FORMAT_MODE_MON:
                        image = np.frombuffer(data, np.uint8).reshape(
                            Height, Width, 1)

                        if logging:
                            curr_logging = True

                            #save ENTIRE IMAGE!!!
                            np.save(loghandle1, image)

                            prev_bin = 0
                            for i in range(log_stepsize, 255, log_stepsize):
                                #print i
                                #print bigger0-bigger20
                                smaller = (image < i).sum()
                                loghandle.write(
                                    str(smaller - prev_bin) + ',' + ' ')
                                prev_bin = smaller
                            #last one:
                            smaller = (image < 256).sum()
                            loghandle.write(
                                str(smaller - prev_bin) + ';' + '\n')

                            curr_num_frames_log += 1

                            if not (curr_num_frames_log <= max_num_frames_log):
                                logging = False
                                loghandle.close()
                                loghandle1.close()
                                print "Logging to" + logname + "has ended"
                                curr_num_frames_log = 0

                            curr_logging = False

                    if emImageFmtMode == ArducamSDK.FORMAT_MODE_RAW:
                        image = np.frombuffer(data, np.uint8).reshape(
                            Height, Width, 1)
                        if color_mode == 0:
                            image = cv2.cvtColor(image, COLOR_BayerRG2BGR)
                        if color_mode == 1:
                            image = cv2.cvtColor(image, COLOR_BayerGR2BGR)
                        if color_mode == 2:
                            image = cv2.cvtColor(image, COLOR_BayerGB2BGR)
                        if color_mode == 3:
                            image = cv2.cvtColor(image, COLOR_BayerBG2BGR)
                        if color_mode < 0 and color_mode > 3:
                            image = cv2.cvtColor(image, COLOR_BayerGB2BGR)

                    #HERE ONLY OUTPUTS CURRENT FPS -> IS THIS REALLY NEEDED?
                    time1 = time.time()
                    if time1 - time0 >= 1:
                        #print "%s %d %s\n"%("fps:",count,"/s")
                        print(time1 - time0)
                        count = 0
                        time0 = time1
                    count += 1

                    # POTENTIALLY SAVES THE IMAGE -> NEEDED?!
                    if save_flag:
                        cv2.imwrite("images/image%d.jpg" % totalFrame, image)
                        totalFrame += 1

                    # Descide if you want to resize or not here!!
                    # ATTENTION: THIS INFLUENCES THE RESULTING FRAME RATE
                    #image = cv2.resize(image,(640,480),interpolation = cv2.INTER_LINEAR)

                    # INCLUDE FCT INN THE FUTURE WHICH PART OF THE IMAGE I WANT TO SEE,...
                    # CROPPING IS HERE:
                    #image = image[0:640, 0:480]

                    # DESCIDE IF THE NORMAL DEMO WINDOW SHOULD OPEN OR NOT
                    # IN LONG EXPOSURE SCENARIO THIS MAKES NO BIG DIFFERENCE!!!
                    cv2.imshow("ArduCam Demo", image)
                    cv2.waitKey(10)

                    #NEWLY INSERTED ROS PUBLISHER

                    try:
                        publisher_img.publish(
                            bridge.cv2_to_imgmsg(image, 'mono8'))
                    except CvBridgeError as e:
                        print(e)

                    #publisher_img.publish(image)

                    # assuming: del deletes the pic,...
                    #ArducamSDK.Py_ArduCam_del(handle)
                    ArducamSDK.Py_ArduCam_flush(handle)
                    #print "------------------------display time:",(time.time() - display_time)
                else:
                    time.sleep(0.001)

                count1 = count1 - 1
                rt_lock.release()
            else:
                time.sleep(0.001)
        else:
            time.sleep(0.001)
示例#3
0
def readImage_thread(publisher_img):
    global handle, running, Width, Height, save_flag, cfg, color_mode, rt_lock, count1
    global COLOR_BayerGB2BGR, COLOR_BayerRG2BGR, COLOR_BayerGR2BGR, COLOR_BayerBG2BGR

    bridge = CvBridge()

    count = 0
    totalFrame = 0
    time0 = time.time()
    time1 = time.time()
    data = {}

    # DEMO WINDOW DECISION!!!
    #cv2.namedWindow("ArduCam Demo",1)

    if not os.path.exists("images"):
        os.makedirs("images")
    while running:
        #if count1 == 1:
        if rt_lock.acquire(False):
            #print "read image", count1
            display_time = time.time()
            if ArducamSDK.Py_ArduCam_availableImage(handle) > 0:
                rtn_val, data, rtn_cfg = ArducamSDK.Py_ArduCam_readImage(
                    handle)
                datasize = rtn_cfg['u32Size']
                if rtn_val != 0:
                    print "read data fail!"
                    continue

                if datasize == 0:
                    continue

                image = None
                emImageFmtMode = cfg['emImageFmtMode']
                if emImageFmtMode == ArducamSDK.FORMAT_MODE_JPG:
                    image = JPGToMat(data, datasize)
                if emImageFmtMode == ArducamSDK.FORMAT_MODE_YUV:
                    image = YUVToMat(data)
                if emImageFmtMode == ArducamSDK.FORMAT_MODE_RGB:
                    image = RGB565ToMat(data)
                if emImageFmtMode == ArducamSDK.FORMAT_MODE_MON:
                    image = np.frombuffer(data,
                                          np.uint8).reshape(Height, Width, 1)
                if emImageFmtMode == ArducamSDK.FORMAT_MODE_RAW:
                    image = np.frombuffer(data,
                                          np.uint8).reshape(Height, Width, 1)
                    if color_mode == 0:
                        image = cv2.cvtColor(image, COLOR_BayerRG2BGR)
                    if color_mode == 1:
                        image = cv2.cvtColor(image, COLOR_BayerGR2BGR)
                    if color_mode == 2:
                        image = cv2.cvtColor(image, COLOR_BayerGB2BGR)
                    if color_mode == 3:
                        image = cv2.cvtColor(image, COLOR_BayerBG2BGR)
                    if color_mode < 0 and color_mode > 3:
                        image = cv2.cvtColor(image, COLOR_BayerGB2BGR)

                #HERE ONLY OUTPUTS CURRENT FPS -> IS THIS REALLY NEEDED?
                time1 = time.time()
                if time1 - time0 >= 1:
                    print "%s %d %s\n" % ("fps:", count, "/s")
                    #print (time1 - time0)
                    count = 0
                    time0 = time1
                count += 1

                # POTENTIALLY SAVES THE IMAGE -> NEEDED?!
                if save_flag:
                    cv2.imwrite("images/image%d.jpg" % totalFrame, image)
                    totalFrame += 1

                # Descide if you want to resize or not here!!
                # ATTENTION: THIS INFLUENCES THE RESULTING FRAME RATE
                #image = cv2.resize(image,(640,480),interpolation = cv2.INTER_LINEAR)

                # INCLUDE FCT INN THE FUTURE WHICH PART OF THE IMAGE I WANT TO SEE,...
                # CROPPING IS HERE:
                #image = image[0:640, 0:480]

                # DEMO WINDOW SHOULD NOT BE OPEN OR HERE, DUE TO FRAME RATE ISSUES,...
                #cv2.imshow("ArduCam Demo",image)
                # cv2.waitKey(10)

                #NEWLY INSERTED ROS PUBLISHER

                try:
                    publisher_img.publish(bridge.cv2_to_imgmsg(image, 'mono8'))
                except CvBridgeError as e:
                    print(e)

                #publisher_img.publish(image)

                # assuming: del deletes the pic,...
                #ArducamSDK.Py_ArduCam_del(handle)
                ArducamSDK.Py_ArduCam_flush(handle)
                #print "------------------------display time:",(time.time() - display_time)
            else:
                time.sleep(0.001)

        #    count1 = count1 - 1
        rt_lock.release()