def writeSensorRegs(fileNodes): global handle for i in range(0, len(fileNodes)): fileNode = fileNodes[i] regAddr = int(fileNode[0], 16) val = int(fileNode[1], 16) ArducamSDK.Py_ArduCam_writeSensorReg(handle, regAddr, val)
def video(): global flag, regArr, handle regNum = 0 res, handle = ArducamSDK.Py_ArduCam_autoopen(cfg) if res == 0: openFlag = True print "device open success!" while (regArr[regNum][0] != 0xFFFF): ArducamSDK.Py_ArduCam_writeSensorReg(handle, regArr[regNum][0], regArr[regNum][1]) regNum = regNum + 1 res = ArducamSDK.Py_ArduCam_beginCapture(handle) if res == 0: print "transfer task create success!" while flag: res = ArducamSDK.Py_ArduCam_capture(handle) if res != 0: print "capture fail!" break time.sleep(0.03) if flag == False: break else: print "transfer task create fail!" res = ArducamSDK.Py_ArduCam_close(handle) if res == 0: openFlag = False print "device close success!" else: print "device close fail!" else: print "device open fail!"
def writeSensorRegs(handle, fileNodes): for i in range(0, len(fileNodes)): fileNode = fileNodes[i] if fileNode[0] == "DELAY": time.sleep(float(fileNode[1]) / 1000) continue regAddr = int(fileNode[0], 16) val = int(fileNode[1], 16) ArducamSDK.Py_ArduCam_writeSensorReg(handle, regAddr, val)
def write_regs(self, reg_name): for r in self.get_register_value(reg_name): if r[0] == "DELAY": time.sleep(float(r[1]) / 1000) continue self.logger.debug("Writing register to cam {0}: {1}".format( self.dev_id, r)) ArducamSDK.Py_ArduCam_writeSensorReg(self.handle, int(r[0], 16), int(r[1], 16))
def show2(): global flag, regArr, handle global W_zoom, H_zoom, V_value, H_value, lx, ly, downFlag, saveFlag, saveNum regNum = 0 res, handle = ArducamSDK.Py_ArduCam_autoopen(cfg) if res == 0: openFlag = True print "device open success!" while (regArr[regNum][0] != 0xFFFF): ArducamSDK.Py_ArduCam_writeSensorReg(handle, regArr[regNum][0], regArr[regNum][1]) regNum = regNum + 1 res = ArducamSDK.Py_ArduCam_beginCapture(handle) if res == 0 and flag: print "transfer task create success!" res = ArducamSDK.Py_ArduCam_capture(handle) if ArducamSDK.Py_ArduCam_available(handle) > 0: print "estou available" res, data = ArducamSDK.Py_ArduCam_read(handle, Width * Height) image = Image.frombuffer("L", (Width, Height), data) img = np.array(image) height, width = img.shape[:2] img2 = cv2.cvtColor(img, COLOR_BYTE2RGB) if args.name and capture: cv2.imwrite(args.name + "." + args.type, img2) if not args.show: return if saveFlag: saveFlag = False saveNum += 1 name = "" if "bmp" == args.type: name = str(saveNum) + ".bmp" if "png" == args.type: name = str(saveNum) + ".png" if "jpg" == args.type: name = str(saveNum) + ".jpg" cv2.imwrite(name, img2) if res != 0: print "capture fail!" return if flag == False: return else: print "transfer task create fail!" res = ArducamSDK.Py_ArduCam_close(handle) if res == 0: openFlag = False print "device close success!" else: print "device close fail!" else: print "device open fail!"
def write_register(request): global handle register = request.register value = request.value rtn_val = ArducamSDK.Py_ArduCam_writeSensorReg(handle,register,value) if rtn_val == 0: output = 'Value %d written to register %d' % (value, register) else: output = 'Invalid register' return WriteRegResponse(output)
def writeSingleSensorReg(reg_str,val_str): global handle regAddr = int(reg_str,16) val = int(val_str,16) ArducamSDK.Py_ArduCam_writeSensorReg(handle,regAddr,val) #check if value sucessfully changed: if (val == ArducamSDK.Py_ArduCam_readSensorReg(handle,regAddr)[1]): print ("SUCCESFULLY CHANGED" + ' ' + hex(regAddr) + ' ' + "TO" + ' ' + hex(val)) else: rospy.logerr ("WRITING COMMAND NOT SUCCESFULL")
def readThread(): global timestamps, images handle = {} data = {} regNum = 0 res, handle = ArducamSDK.Py_ArduCam_autoopen(cfg) #print(handle) #print(3) if res == 0: print("device open success!") while (regArr[regNum][0] != 0xFFFF): ArducamSDK.Py_ArduCam_writeSensorReg(handle, regArr[regNum][0], regArr[regNum][1]) regNum = regNum + 1 #print(32143) res = ArducamSDK.Py_ArduCam_beginCapture(handle) if res == 0: print("transfer task create success!") while not rospy.is_shutdown(): #print(2134124) res = ArducamSDK.Py_ArduCam_capture(handle) #print(res) if res == 0: timestamp = rospy.Time.now() #print(7324627864) res, data = ArducamSDK.Py_ArduCam_read(handle, Width * Height) #print(34242) if res == 0: #print(3746827346782) ArducamSDK.Py_ArduCam_del(handle) #print(374623846278) timestamps.put(timestamp) #print(3742846237846) images.put(data) #print(382746278346782) time.sleep(0.03) else: print("read data fail!") if res != 0: print("capture fail!") break else: print("transfer task create fail!") res = ArducamSDK.Py_ArduCam_close(handle) if res == 0: print("device close success!") else: print("device close fail!") else: print("device open fail!")
def writeSensorRegs(fileNodes): global handle for i in range(0,len(fileNodes)): fileNode = fileNodes[i] if fileNode[0] == "DELAY": time.sleep(float(fileNode[1])/1000) continue regAddr = int(fileNode[0],16) val = int(fileNode[1],16) print(str(regAddr) + "\t" + str(val)) #["0x3012","0x0032"] = 12306 50 #3012 (hex) = 12306 (dec) #0032 (hex) = 50 (dec) ArducamSDK.Py_ArduCam_writeSensorReg(handle,regAddr,val)
def init_and_read_arducam(): global flag, regArr, handle, openFlag regNum = 0 res, handle = ArducamSDK.Py_ArduCam_autoopen(cfg) if res == 0: openFlag = True print("device open success!") while (regArr[regNum][0] != 0xFFFF): ArducamSDK.Py_ArduCam_writeSensorReg(handle, regArr[regNum][0], regArr[regNum][1]) regNum = regNum + 1 res = ArducamSDK.Py_ArduCam_beginCapture(handle) if res == 0: print("transfer task create success!") while flag: res = ArducamSDK.Py_ArduCam_capture(handle) if res != 0: print("capture failed!") flag = False break time.sleep(0.1) if flag == False: break else: print("transfer task create fail!") time.sleep(2) res = ArducamSDK.Py_ArduCam_close(handle) if res == 0: openFlag = False print("device close success!") else: print("device close fail!") else: print("device open fail!")
try: arduino_write_read(0, 0) # Wait for the Arduino to be initialized except Exception: pass time.sleep(1) show_help() config_overview = "./config/3664_2748.cfg" config_focused = "./config/916_686.cfg" ret, handle = camera_initFromFile(config_overview) if ret: ArducamSDK.Py_ArduCam_setMode(handle, ArducamSDK.CONTINUOUS_MODE) ArducamSDK.Py_ArduCam_writeSensorReg(handle, 0x0202, 200) print("Middle led: %d." % (LED_MIDDLE)) arduino_write_read(LED_MIDDLE, 0) ct = threading.Thread(target=captureImage_thread) ct.start() parameters = None if os.path.exists(settings_file): with open(settings_file) as json_file: parameters = json.load(json_file) mouse_x = parameters["mouse_x"] mouse_y = parameters["mouse_y"] draw_x = parameters["draw_x"]
def set_gain(self, gain): #assert False, "NOT IMPLEMENTED" #self.camera.set_gain(gain) #0x20 = 32 = no gain, ArducamSDK.Py_ArduCam_writeSensorReg(self.handle, 0x305e, round(32 * gain))
def set_exposure(self, exposure): #self.camera.set_exposure_time(exposure)#us setval = round(exposure / 22.22) print(setval) ArducamSDK.Py_ArduCam_writeSensorReg(self.handle, 0x3012, setval) print(ArducamSDK.Py_ArduCam_readSensorReg(self.handle, 0x3012))
def readImage_thread(): global handle, running, width, height, save_multiview, LED_DROP, save_single_flag, save_flag, save_beginning, save_raw, cfg, color_mode, \ calibrate_flag, calibrate_grey, calibrate_at, calibrate_target, calibrate_color, calibrate_results time0 = time.time() time1 = time.time() single_count = 0 count = 0 totalFrame = 0 data = {} cv2.namedWindow("ArduCam output", 1) if not os.path.exists("images"): os.makedirs("images") while running: 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 or datasize == 0: ArducamSDK.Py_ArduCam_del(handle) print("Failed to read data.") continue image = ImageConvert.convert_image(data, rtn_cfg, color_mode) time1 = time.time() interval = 3 # compute framerate every [interval] seconds if time1 - time0 >= interval: if save_flag: elapsed_time = time.time() - save_beginning print( "Frames per second: %d/s (#%d to #%d), elapsed time: %ds." % (count / interval, totalFrame - count, totalFrame, elapsed_time)) else: print("Frames per second: %d/s." % (count / interval)) count = 0 time0 = time1 count += 1 if LED_DROP > 0: LED_DROP -= 1 else: if calibrate_flag is not None: if calibrate_grey is None: # first pass calibrate_grey = np.median(image) print("Target grey: %f." % (calibrate_grey)) elif calibrate_flag >= 0: target, color = get_multiview_components( calibrate_flag) if target != calibrate_target or color != calibrate_color: calibrate_target = target calibrate_color = color arduino_write_read(target, color) ArducamSDK.Py_ArduCam_writeSensorReg( handle, 0x0202, calibrate_at) LED_DROP = 1 else: current_grey = np.median(image) if current_grey + calibrate_threshold >= calibrate_grey or calibrate_at >= calibrate_cap: print( "Calibration result for led %d and color %d (raw: %d): %d (current grey: %f)." % (target, color, calibrate_flag, calibrate_at, current_grey)) if target not in calibrate_results.keys(): calibrate_results[target] = { color: calibrate_at } else: calibrate_results[target][ color] = calibrate_at calibrate_at = calibrate_start calibrate_flag -= 1 else: calibrate_at += calibrate_increase # TODO: adaptative increase ArducamSDK.Py_ArduCam_writeSensorReg( handle, 0x0202, calibrate_at) LED_DROP = 1 else: print("Middle led: %d." % (LED_MIDDLE)) arduino_write_read(LED_MIDDLE, 0) ArducamSDK.Py_ArduCam_writeSensorReg( handle, 0x0202, coarse_integration) LED_DROP = 1 print("Calibration finished.") calibrate_flag = None elif save_multiview is not None: previous = save_multiview + 1 if save_multiview >= 0: target, color = get_multiview_components( save_multiview) if target in calibrate_results.keys(): integration = calibrate_results[target][color] else: integration = coarse_integration arduino_write_read(target, color) ArducamSDK.Py_ArduCam_writeSensorReg( handle, 0x0202, integration) LED_DROP = 1 if save_multiview != LED_MAX_ITERATIONS: # first pass target, color = get_multiview_components( previous) # previous if target in calibrate_results.keys(): integration = calibrate_results[target][color] else: integration = coarse_integration grey = np.median(image) cv2.imwrite( "images/_multi_%d_%d.png" % (target, color), image) print( "Multiview image with led %d and color %d saved (raw: %d), with integration time: %d (grey: %f)." % (target, color, previous, integration, grey)) save_multiview -= 1 else: print("Middle led: %d." % (LED_MIDDLE)) arduino_write_read(LED_MIDDLE, 0) ArducamSDK.Py_ArduCam_writeSensorReg( handle, 0x0202, coarse_integration ) # reset integration to base value LED_DROP = 1 target, color = get_multiview_components( previous) # previous cv2.imwrite( "images/_multi_%d_%d.png" % (target, color), image) print( "Multiview image with led %d and color %d saved (raw: %d)." % (target, color, previous)) save_multiview = None else: if save_single_flag: cv2.imwrite("images/_single%d.png" % single_count, image) print("Single image #%d saved." % (single_count)) single_count += 1 save_single_flag = False if save_flag: cv2.imwrite("images/image%d.png" % totalFrame, image) if save_raw: with open("images/image%d.raw" % totalFrame, 'wb') as f: f.write(data) totalFrame += 1 image = cv2.resize(image, (width, height), interpolation=cv2.INTER_LINEAR) cv2.imshow("ArduCam output", image) cv2.waitKey(10) ArducamSDK.Py_ArduCam_del(handle) else: time.sleep(0.001)
def camera_initFromFile(fileName,index): global cfg,Width,Height,color_mode,save_raw #load config file config = arducam_config_parser.LoadConfigFile(fileName) camera_parameter = config.camera_param.getdict() Width = camera_parameter["WIDTH"] Height = camera_parameter["HEIGHT"] BitWidth = camera_parameter["BIT_WIDTH"] ByteLength = 1 if BitWidth > 8 and BitWidth <= 16: ByteLength = 2 save_raw = True FmtMode = camera_parameter["FORMAT"][0] color_mode = camera_parameter["FORMAT"][1] I2CMode = camera_parameter["I2C_MODE"] I2cAddr = camera_parameter["I2C_ADDR"] TransLvl = camera_parameter["TRANS_LVL"] cfg = {"u32CameraType":0x00, "u32Width":Width,"u32Height":Height, "usbType":0, "u8PixelBytes":ByteLength, "u16Vid":0, "u32Size":0, "u8PixelBits":BitWidth, "u32I2cAddr":I2cAddr, "emI2cMode":I2CMode, "emImageFmtMode":FmtMode, "u32TransLvl":TransLvl } # ArducamSDK. ret,handle,rtn_cfg = ArducamSDK.Py_ArduCam_open(cfg,index) #ret,handle,rtn_cfg = ArducamSDK.Py_ArduCam_autoopen(cfg) if ret == 0: #ArducamSDK.Py_ArduCam_writeReg_8_8(handle,0x46,3,0x00) usb_version = rtn_cfg['usbType'] #print("USB VERSION:",usb_version) configs = config.configs configs_length = config.configs_length for i in range(configs_length): type = configs[i].type if ((type >> 16) & 0xFF) != 0 and ((type >> 16) & 0xFF) != usb_version: continue if type & 0xFFFF == arducam_config_parser.CONFIG_TYPE_REG: ArducamSDK.Py_ArduCam_writeSensorReg(handle, configs[i].params[0], configs[i].params[1]) elif type & 0xFFFF == arducam_config_parser.CONFIG_TYPE_DELAY: time.sleep(float(configs[i].params[0])/1000) elif type & 0xFFFF == arducam_config_parser.CONFIG_TYPE_VRCMD: configBoard(handle, configs[i]) rtn_val,datas = ArducamSDK.Py_ArduCam_readUserData(handle,0x400-16, 16) print("Serial: %c%c%c%c-%c%c%c%c-%c%c%c%c"%(datas[0],datas[1],datas[2],datas[3], datas[4],datas[5],datas[6],datas[7], datas[8],datas[9],datas[10],datas[11])) return handle else: print("open fail,ret_val = ",ret) return None
def camera_initFromFile(self, mainconf, expconf): self.expconf = expconf self.fileName = mainconf config = arducam_config_parser.LoadConfigFile(self.fileName) camera_parameter = config.camera_param.getdict() self.Width = camera_parameter["WIDTH"] self.Height = camera_parameter["HEIGHT"] self.BitWidth = camera_parameter["BIT_WIDTH"] self.ByteLength = 1 if self.BitWidth > 8 and self.BitWidth <= 16: self.ByteLength = 2 self.FmtMode = camera_parameter["FORMAT"][0] self.color_mode = camera_parameter["FORMAT"][1] # print("color mode",self.color_mode) self.I2CMode = camera_parameter["I2C_MODE"] self.I2cAddr = camera_parameter["I2C_ADDR"] self.TransLvl = camera_parameter["TRANS_LVL"] self.cfg = { "u32CameraType": 0x00, "u32Width": self.Width, "u32Height": self.Height, "usbType": 0, "u8PixelBytes": self.ByteLength, "u16Vid": 0, "u32Size": 0, "u8PixelBits": self.BitWidth, "u32I2cAddr": self.I2cAddr, "emI2cMode": self.I2CMode, "emImageFmtMode": self.FmtMode, "u32TransLvl": self.TransLvl } ret, self.handle, self.rtn_cfg = ArducamSDK.Py_ArduCam_autoopen( self.cfg) if ret == 0: usb_version = self.rtn_cfg['usbType'] configs = config.configs configs_length = config.configs_length for i in range(configs_length): # print(configs[i].params[0]) type = configs[i].type if ((type >> 16) & 0xFF) != 0 and ( (type >> 16) & 0xFF) != usb_version: continue if type & 0xFFFF == arducam_config_parser.CONFIG_TYPE_REG: if configs[i].params[0] == 12306: configs[i].params[1] = self.config_exposure( self.expconf) ArducamSDK.Py_ArduCam_writeSensorReg( self.handle, configs[i].params[0], configs[i].params[1]) elif type & 0xFFFF == arducam_config_parser.CONFIG_TYPE_DELAY: time.sleep(float(configs[i].params[0]) / 1000) elif type & 0xFFFF == arducam_config_parser.CONFIG_TYPE_VRCMD: self.configBoard(configs[i]) rtn_val, datas = ArducamSDK.Py_ArduCam_readUserData( self.handle, 0x400 - 16, 16) print( "Serial: %c%c%c%c-%c%c%c%c-%c%c%c%c" % (datas[0], datas[1], datas[2], datas[3], datas[4], datas[5], datas[6], datas[7], datas[8], datas[9], datas[10], datas[11])) return True else: print("open fail,rtn_val = ", ret) return False
def camera_initFromFile(fileName, p_width=None, p_height=None): global cfg, handle, width, height, color_mode, save_raw config = arducam_config_parser.LoadConfigFile(fileName) camera_parameter = config.camera_param.getdict() if p_width is None: width = camera_parameter["WIDTH"] else: width = p_width if p_height is None: height = camera_parameter["HEIGHT"] else: height = p_height BitWidth = camera_parameter["BIT_WIDTH"] ByteLength = 1 if BitWidth > 8 and BitWidth <= 16: ByteLength = 2 save_raw = True FmtMode = camera_parameter["FORMAT"][0] color_mode = camera_parameter["FORMAT"][1] print("Color mode: %d." % (color_mode)) I2CMode = camera_parameter["I2C_MODE"] I2cAddr = camera_parameter["I2C_ADDR"] TransLvl = camera_parameter["TRANS_LVL"] cfg = { "u32CameraType": 0x00, "u32Width": width, "u32Height": height, "usbType": 0, "u8PixelBytes": ByteLength, "u16Vid": 0, "u32Size": 0, "u8PixelBits": BitWidth, "u32I2cAddr": I2cAddr, "emI2cMode": I2CMode, "emImageFmtMode": FmtMode, "u32TransLvl": TransLvl } ret, handle, rtn_cfg = ArducamSDK.Py_ArduCam_autoopen(cfg) if ret == 0: usb_version = rtn_cfg['usbType'] configs = config.configs configs_length = config.configs_length for i in range(configs_length): type = configs[i].type if ((type >> 16) & 0xFF) != 0 and ( (type >> 16) & 0xFF) != usb_version: continue if type & 0xFFFF == arducam_config_parser.CONFIG_TYPE_REG: ArducamSDK.Py_ArduCam_writeSensorReg(handle, configs[i].params[0], configs[i].params[1]) elif type & 0xFFFF == arducam_config_parser.CONFIG_TYPE_DELAY: time.sleep(float(configs[i].params[0]) / 1000) elif type & 0xFFFF == arducam_config_parser.CONFIG_TYPE_VRCMD: configBoard(configs[i]) rtn_val, datas = ArducamSDK.Py_ArduCam_readUserData( handle, 0x400 - 16, 16) print("Serial: %c%c%c%c-%c%c%c%c-%c%c%c%c." % (datas[0], datas[1], datas[2], datas[3], datas[4], datas[5], datas[6], datas[7], datas[8], datas[9], datas[10], datas[11])) return True, handle else: print("Failed to open, return value: %s." % (ret)) return False, handle
def camera_initFromFile(fileName): global cfg, handle, Width, Height, color_mode #load config file config = arducam_config_parser.LoadConfigFile(fileName) camera_parameter = config.camera_param.getdict() Width = camera_parameter["WIDTH"] Height = camera_parameter["HEIGHT"] BitWidth = camera_parameter["BIT_WIDTH"] ByteLength = 1 if BitWidth > 8 and BitWidth <= 16: ByteLength = 2 FmtMode = camera_parameter["FORMAT"][0] color_mode = camera_parameter["FORMAT"][1] print "color mode", color_mode I2CMode = camera_parameter["I2C_MODE"] I2cAddr = camera_parameter["I2C_ADDR"] TransLvl = camera_parameter["TRANS_LVL"] cfg = { "u32CameraType": 0x00, "u32Width": Width, "u32Height": Height, "usbType": 0, "u8PixelBytes": ByteLength, "u16Vid": 0, "u32Size": 0, "u8PixelBits": BitWidth, "u32I2cAddr": I2cAddr, "emI2cMode": I2CMode, "emImageFmtMode": FmtMode, "u32TransLvl": TransLvl } # ArducamSDK. if serial_selection == None: ret, handle, rtn_cfg = ArducamSDK.Py_ArduCam_autoopen(cfg) else: index = None num_cam, cam_list, cam_serial = ArducamSDK.Py_ArduCam_scan() for i in range(num_cam): datas = cam_serial[i] camera_serial = "%c%c%c%c-%c%c%c%c-%c%c%c%c" % ( datas[0], datas[1], datas[2], datas[3], datas[4], datas[5], datas[6], datas[7], datas[8], datas[9], datas[10], datas[11]) if camera_serial == str(serial_selection): print("Arducam " + str(serial_selection) + " found") index = i break if index == None: print("Arducam " + str(serial_selection) + " not found") exit() time.sleep(3) ret, handle, rtn_cfg = ArducamSDK.Py_ArduCam_open(cfg, i) if ret == 0: usb_version = rtn_cfg['usbType'] print "USB VERSION:", usb_version configs = config.configs configs_length = config.configs_length for i in range(configs_length): type = configs[i].type if ((type >> 16) & 0xFF) != 0 and ( (type >> 16) & 0xFF) != usb_version: continue if type & 0xFFFF == arducam_config_parser.CONFIG_TYPE_REG: ArducamSDK.Py_ArduCam_writeSensorReg(handle, configs[i].params[0], configs[i].params[1]) elif type & 0xFFFF == arducam_config_parser.CONFIG_TYPE_DELAY: time.sleep(float(configs[i].params[0]) / 1000) elif type & 0xFFFF == arducam_config_parser.CONFIG_TYPE_VRCMD: configBoard(configs[i]) rtn_val, datas = ArducamSDK.Py_ArduCam_readUserData( handle, 0x400 - 16, 16) print "Serial: %c%c%c%c-%c%c%c%c-%c%c%c%c" % ( datas[0], datas[1], datas[2], datas[3], datas[4], datas[5], datas[6], datas[7], datas[8], datas[9], datas[10], datas[11]) return True else: print "open fail,rtn_val = ", ret return False