def create_dataset_testfile(dir_path, out_path): subdir_list = os.listdir(dir_path) sampler = TestSampler() plt.ion() plt.figure(1) for name in subdir_list: print('start process {}:'.format(name)) cnt = 0 fp = open(os.path.join(out_path, str(name) + '.pkl'), 'wb') img_namelist = os.listdir(os.path.join(dir_path, name)) img_namelist = np.sort(img_namelist) for img in img_namelist: img_path = os.path.join(dir_path, name, img) img_data = cv2.imread(img_path, cv2.IMREAD_COLOR) img_data = cv2.resize(img_data, (640, 480)) #img_data=np.copy(img_data[:,:,::-1]) # print(img_data.shape) # print(img_data) sample_list, scaled = sampler.sample_and_filter(img_data) plt.imshow(scaled[:,:,::-1]) plt.draw() plt.show() obj = {'origin': utils.encode_img(img_data), 'scaled': utils.encode_img(scaled)} pickle.dump(obj, fp, -1) for sample in sample_list: pickle.dump(sample, fp, -1) cnt = cnt + 1 print(cnt) fp.close()
def sampling_process(self,data,scaled,shape): """ 采用随机性策略抽取样本,缺少样本数量的确定性,但是可以很大程度加快处理速度,视情况采用 :param 输入的原始图像矩阵,要求格式为(H,W,C)通道最好为RGB :param scaled: 缩放(4倍)后的图像矩阵 :return: 抽样得到的样本列表 """ sample_list = [] #if self.cache0 is None or self.cache1 is None: #return sample_list dx=dy=20 self.cache.append((data,scaled,shape)) for i in range(shape[0]//dy): for j in range(shape[1]//dx): #lr = scaled[i * dx:(i + 1) * dx, j * dy:(j + 1) * dy, :] if np.random.rand()<self.sample_rate:#*self.get_weight(lr): LQs = [] GT = [] #self.logger.info('cache len is {}'.format(len(self.cache))) for index in range(len(self.cache)): GT.append(self.cache[index][0][i * dx * 4:(i + 1) * dx * 4, j * dy * 4:(j + 1) * dy * 4, :]) if index%2==0: LQs.append(self.cache[index][1][i * dx:(i + 1) * dx, j * dy:(j + 1) * dy, :]) sample = {'LQs': [utils.encode_img(lr) for lr in LQs], 'GT': [utils.encode_img(hr) for hr in GT] ,'sample':True} sample_list.append(sample) self.cache.clear() self.cache.append((data,scaled,shape)) #chean sample cache return sample_list
def create_PSNR_testfile(self,file_path): """ 这部分或许可以直接在服务端验证 :param file_path: :return: """ f_path=Path(file_path) if not f_path.parent.exists(): f_path.parent.mkdir(parents=True) sampler=TestSampler() fp=open(file_path,'wb') with picamera.PiCamera() as camera: rawFrame = PiRGBArray(camera, (640, 480)) camera.resolution = (640, 480) camera.framerate = 20 shape=(640,480) time.sleep(2) self.start = time.time() cnt=0 start=time.time() for frame in camera.capture_continuous(rawFrame, 'rgb', use_video_port=True): origin = np.array(frame.array, dtype=np.uint8) sample_list, scaled = sampler.sample_and_filter(origin) h_n = int(80 * np.ceil(origin.shape[0] / 80)) w_n = int(80 * np.ceil(origin.shape[1] / 80)) normed = np.zeros((h_n, w_n, 3), dtype=np.float32) / 255. normed[0:origin.shape[0], 0:origin.shape[1], :] = origin obj = {'origin': utils.encode_img(normed), 'scaled': utils.encode_img(scaled),'shape':shape} pickle.dump(obj, fp, -1) for sample in sample_list: pickle.dump(sample,fp,-1) cnt = cnt + 1 if cnt%10: self.logger.info("sent {} frames in {} sec,fps:{}".format(cnt, time.time() - start, cnt / (time.time() - start))) if cnt>200: break self.logger.info(cnt) self.count = self.count + 1 rawFrame.seek(0) rawFrame.truncate() if self.terminate: self.logger.info('terminate') break fp.close()
def create_datafile(self,file_path): """ create the original captured frames,encoded in png :param file_path: the path of the data pkl file """ f_path=Path(file_path) if not f_path.parent.exists(): f_path.parent.mkdir(parents=True) sampler=TestSampler(sample_len=7) fp=open(file_path,'wb') with picamera.PiCamera() as camera: rawFrame = PiRGBArray(camera, (640, 480)) camera.resolution = (640, 480) camera.framerate = 15 shape=(640,480) time.sleep(2) self.start = time.time() cnt=0 start=time.time() prev_cnt=0 len_sum=0 for frame in camera.capture_continuous(rawFrame, 'rgb', use_video_port=True): frame = np.array(frame.array, dtype=np.uint8) #frame = cv2.resize(frame, dsize=(0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_AREA) # frame=utils.imresize_np(frame,1/4,True) pickle.dump(utils.encode_img(frame), fp, -1) # sample_list, scaled = sampler.sample_and_filter(np.array(frame.array, dtype=np.uint8)) # #self.logger.info('len of sample list is {}'.format(len(sample_list))) # len_sum=len_sum+len(sample_list) # obj={'img':utils.encode_img(scaled),'shape':shape,'sample':False} # pickle.dump(obj,fp,-1) # # for sample in sample_list: # pickle.dump(sample,fp,-1) # # self.logger.info('send sample') cnt = cnt + 1 self.count = self.count + 1 if cnt%10==0: self.logger.info('total sample num is:{}'.format(len_sum)) self.logger.info("sent {} frames in {} sec,fps:{}".format(cnt, time.time() - start, cnt / (time.time() - start))) start=time.time() cnt=0 len_sum=0 if self.count>200: break #self.logger.info(cnt) rawFrame.seek(0) rawFrame.truncate() if self.terminate: self.logger.info('terminate') break fp.close()
def create_dataset_testfile(self,dir_path,out_path): subdir_list=os.listdir(dir_path) sampler=TestSampler() plt.ion() plt.figure(1) for name in subdir_list: self.logger.info('start process {}:'.format(name)) cnt=0 fp=open(os.path.join(out_path,str(name)+'.pkl'),'wb') img_namelist=os.listdir(os.path.join(dir_path,name)) img_namelist= np.sort(img_namelist) for img in img_namelist: img_path=os.path.join(dir_path,name,img) img_data=cv2.imread(img_path,cv2.IMREAD_COLOR) shape=(img_data.shape[1],img_data.shape[0]) #img_data=cv2.resize(img_data,(640,480)) h_n = int(80 * np.ceil(img_data.shape[0] / 80)) w_n = int(80 * np.ceil(img_data.shape[1] / 80)) normed = np.zeros((h_n, w_n, 3), dtype=np.float32) / 255. normed[0:img_data.shape[0], 0:img_data.shape[1], :] = img_data plt.imshow(img_data) plt.draw() plt.show() #print(img_data.shape) #print(img_data) sample_list, scaled = sampler.sample_and_filter(img_data) obj = {'origin': utils.encode_img(img_data), 'scaled': utils.encode_img(scaled),'shape':shape} pickle.dump(obj, fp, -1) for sample in sample_list: pickle.dump(sample, fp, -1) cnt = cnt + 1 self.logger.info(cnt) self.count = self.count + 1 fp.close()
def root(): print('the request:', request) x = request.get_json() img = x['input'] img = decode_img(img) predictions = model(img) img = visualise_keypoints(img, predictions) img = encode_img(img) return json.dumps({ 'statusCode': 200, 'response': img, 'headers': { 'Access-Control-Allow-Origin': '*' } })
def sample_thread(sock, capture_pool): sampler = TestSampler() logger = logging.getLogger('base') cnt = 0 while cnt < 20: with capture_pool.pool_lock: if capture_pool.pool: img = capture_pool.pool.pop() sample_list, scaled = sampler.sample_and_filter(img) obj = { 'img': utils.encode_img(scaled), 'shape': img.shape, 'sample': False } network.send_obj(sock, obj) for sample in sample_list: network.send_obj(sock, sample) cnt = cnt + 1 logger.info(cnt) else: time.sleep(0.02)
def start_client_with_file(ip_addr, port, file_path): """ 利用pkl文件来模拟服务端的数据采集 :param ip_addr: 服务端的ip地址 :param port: 服务器端口 :param file_path: 待使用的pkl文件位置 """ logger = logging.getLogger('base') logger.info('start_client') fp = open(file_path, 'rb') conn = connection(ip_addr, port) conn.start_connection() cnt = 0 sampler = dataprocess.Sampler.TestSampler() start = time.time() while True: try: data = pickle.load(fp) data = utils.decode_jpeg(data) sample_list, scaled = sampler.sample_and_filter(data) obj = { 'img': utils.encode_img(scaled), 'shape': data.shape, 'sample': False } network.send_obj(conn.socket, obj) for sample in sample_list: network.send_obj(conn.socket, sample) # TODO:考虑还有没有优化的策略 cnt = cnt + 1 if cnt % 10 == 0: curr_time = time.time() logger.info("sent {} frames in {} sec,fps:{}".format( cnt, curr_time - start, cnt / (curr_time - start))) except EOFError: break conn.stop_connection() fp.close()
def write_predict(raw_image, graph, fps): with tf.Session(graph=graph) as sess: # Encode test image raw_img, test_input = encode_img(raw_image, MODEL_SIZE) print('test_input shape', test_input.shape) # Run tf model pred = sess.run(y, feed_dict={x: test_input}) # Handle model output boxes, scores, classes, nums = output_boxes( \ pred, MODEL_SIZE, max_output_size=MAX_OUTPUT_SIZE, max_output_size_per_class=MAX_OUTPUT_SIZE_PER_CLASS, iou_threshold=IOU_THRESHOLD, confidence_threshold=CONFIDENCE_THRESHOLD) img = draw_outputs(raw_img, boxes, scores, classes, nums, class_names) # Add fps value words_color = (0, 0, 255) #BGR if fps is not None: cv2.putText(img, "FPS: {:.2f}".format(fps), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, words_color, 1) # Write final result cv2.imwrite('result.jpg', img) print('scores', scores.eval())
def start_capture_no_bolck(self,sock): self.srhandler=SRHandler(sock,self.sampler) self.srhandler.setDaemon(True) self.srhandler.start()#开启接收线程 with picamera.PiCamera() as camera: rawFrame = PiRGBArray(camera, (640, 480)) camera.resolution = (640, 480) camera.framerate = 15 #目前能够到达17fps左右的采集速度,推测此前瓶颈在于网络传输 shape=(640,480) time.sleep(2) self.start = time.time() cnt=0 start=self.start for frame in camera.capture_continuous(rawFrame, 'rgb', use_video_port=True): frame=np.array(frame.array, dtype=np.uint8) sample_list, scaled = self.sampler.sample_and_filter(frame) obj={'img':utils.encode_img(scaled),'shape':shape,'sample':False} network.send_obj(sock,obj) for sample in sample_list: network.send_obj(sock, sample) #TODO:考虑还有没有优化的策略 cnt = cnt + 1 if cnt%10==0: curr_time=time.time() self.logger.info("sent {} frames in {} sec,fps:{}".format(cnt, curr_time - start, cnt / (curr_time - start))) if cnt>200: self.logger.info('process end') break #self.logger.info(cnt) self.count = self.count + 1 rawFrame.seek(0) rawFrame.truncate() if self.terminate: self.logger.info('terminate') break
import requests from PIL import Image import json from io import BytesIO import base64 from utils import encode_img, decode_img from time import time host = '18.133.254.35' host = 'localhost' img = Image.open('Images/walking.jpg') img = encode_img(img) s = { 'input': img } r = requests.post(f'http://{host}:5000/', data=json.dumps(s), headers={'content-type': 'application/json'}) print(r) print(r.json()) r = r.json() img = decode_img(r['response']) img.show()
def get_id(image): return encode_img("db/" + image + ".jpg")
def __make_base64_encoded_string(self, img_path): encoded_img = utils.encode_img(img_path) return utils.process_base64(encoded_img)