def receiver(client, addr): global counter counter += 1 # 스레드 별로 imshow에서 사용할 타이틀을 다르게 하기 위해서 frame_name = f'frame{counter}' reader = client.makefile('rb') writer = client.makefile('wb') while True: data, data_len = net2.receive(reader) if not data: break # print('received ', data_len) # 이미지 처리 # AI 알고리즘 처리 - 불량 여부 판단... 여기에 넣으면됭 show_image(data, frame_name) result = json.dumps({'result': 'ok'}) net2.send(writer, result.encode()) print('exit receiver')
def receiver(client, addr): reader = client.makefile('rb') # writer = client.makefile('wb') while True: data, data_len = net2.receive(reader) print('-------수신 ') if not data_len: return # data : jpeg 이미지 # image : bgr 이미지 ret, image = show_image(data) # AI 알고리즘 처리 cv2.imshow('image', image) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows() print('exit receiver')
def vido_streaming(): with socket.socket() as s: s.connect((HOST,PORT)) writer = s.makefile('wb') reader = s.makefile('rb') stream=io.BytesIO() for _ in camera.capture_continuous(stream,'jpeg',use_video_port=True): image=stream.getvalue() net2.send(writer,image) result=net2.receive(reader)[0] stream.seek(0) stream.truncate() if not button.is_pressed: writer.write(struct.pack('<L',0)) writer.flush() break
def receiver(client, addr): global count # count += 1 filename = count reader = client.makefile('rb') # writer = client.makefile('wb') data, data_len = net2.receive(reader) # print('-------수신 ') if not data_len: return # data : jpeg 이미지 # image : bgr 이미지 image, key = show_image(data) # cv2.imwrite("test" + str(count) + ".jpg" , image) # print(image) # AI 알고리즘 처리 cv2.imshow(str(filename), image) cv2.waitKey(0) cv2.destroyAllWindows()
def receiver(client, addr , model): print('\n\n\n#####################################################\n') asyncio.set_event_loop(asyncio.new_event_loop()) # �ð� �������� �������� current_time = datetime.now() + timedelta(hours=9) current_time = current_time.strftime("%m%d%H%M%S.%f")[:-3] current_time = current_time.replace("." , "") print("#####################") print(current_time) print("#####################") ############################### # 1. �̹��� ���� �� ���� # ############################### print("="*40) print('Receiver start') print("="*40) reader = client.makefile('rb') data, data_len = net2.receive(reader) if not data_len: return image, key = show_image(data) # ī�� ��Ʈ �̹��� file_name = "test" cv2.imwrite("./" + file_name + ".jpg" , image) ############################### # 2. AI - �з� # ############################### ## YOLO ���� NAME_CFG = "yolov4_CASHER_v6.cfg" NAME_WEIGHT = "yolov4_CASHER_v6_best.weights" os.system('./darknet detector test data/obj.data ' + NAME_CFG + " " + NAME_WEIGHT + " test.jpg" + " -thresh 0.5 -out result.json") os.system('mv predictions.jpg ./predictions/' + "result_" + current_time + ".jpg") os.system('mv result.json ./predictions/' + "result_" + current_time + ".json") ## OBJECT DETECTION ��� json_file_name = "result_" + current_time + ".json" with open('./predictions/' + json_file_name , 'r') as f: json_data = json.load(f) print(json_data) THRESHOLD = 0.7 ITEM_LST = [] RESULT = 1 for item_data in json_data[0]["objects"]: item_dict = {} item_dict["id"] = item_data["name"] item_dict["prob"] = item_data["confidence"] item_dict["outcome"] = 1 if item_dict["prob"] > THRESHOLD : item_dict["outcome"] = 1 else: item_dict["outcome"] = 0 RESULT = 0 ITEM_LST.append(item_dict) # ############################### # # 3. Ŭ���� ���� �۽� # # ############################### print("="*40) print('Send Server start') print("="*40) ## 1. REST_API >> S3 ��� REST_API_DICT = {} REST_API_DICT["result"] = RESULT REST_API_DICT["time"] = current_time REST_API_DICT["item"] = ITEM_LST response_1 = send_REST_API(REST_API_DICT) print("REST-API SERVER") print(response_1) ## 3. BOTO3 >> S3 ��� if RESULT == False: response_2 = send_S3( "./predictions/" + "result_" + current_time + ".jpg" , "result_" + current_time + ".jpg" ) print("BOTO3 SERVER") print(response_2) ## 4. WEBSOCKET >> WEB ��� WEB_DICT = {} WEB_LST = [] for item in ITEM_LST: item_dict = {} item_dict["id"] = YOLO_CONVERT_INDICATOR[item["id"]] item_dict["name"] = DF[DF["Item_id"]==int(item_dict["id"])]["Product_name"].values[0] item_dict["price"] = int(DF[DF["Item_id"]==int(item_dict["id"])]["Price"].values[0]) item_dict["Qty"] = 1 WEB_LST.append(item_dict) WEB_DICT["item"] = WEB_LST asyncio.get_event_loop().run_until_complete(connect(WEB_DICT))
def receiver(client, addr , model): #print('\n\n\n#####################################################\n') asyncio.set_event_loop(asyncio.new_event_loop()) ############################### # 1. 이미지 수신 및 저장 # ############################### print('\n\n\n') print("="*100) print('Receiver start') print("="*100) reader = client.makefile('rb') data, data_len = net2.receive(reader) if not data_len: return image, key = show_image(data) # 현재 시간 기준으로 파일명 저장 current_time = time.time() current_ms = str(int(round(current_time * 1000)))[-3:] current_time = time.strftime(f'%m%d%H%M%S{current_ms}', time.localtime(current_time)) file_name = "result_" + current_time + '.jpg' # result_0601135334123.jpg (6월 1일 13시 53분 34.123초) print(f'\n#\n# File Name : {file_name}\n#\n') cv2.imwrite("./History/" + file_name, image) ############################### # 2. AI - 분류 # ############################### THRESHHOLD = 0.9 # 이미지 Resize input_arr = resize(image, (200,200)) / 255 input_arr = np.array([input_arr]) # Convert single image to a batch. input_arr = input_arr[:,:,:,::-1] # GBR to RGB # Model predict prob = MODEL.predict(input_arr)[0] pred = prob.argmax() prob = prob[pred] # 더미값 primary_key = int(indicate_key[pred]) result = 1 if prob > THRESHHOLD else 0 print('\n#\n# 예측 결과\n#\n') print(f'Predict Index : {pred} name : {indicate[pred]}') print(f'Probability : {prob}') print(f'Primary Key : {primary_key}\n') ############################### # 3. 클라우드 서버 송신 # ############################### ## 1. SOCKET >> IOT 통신 if FAIL_IOT: print('Start IoT socket...') sendData = str(result) connectionSock.send(sendData.encode('utf-8')) connectionSock.send(sendData.encode('utf-8')) # 클라우드 서버에 송신할 데이터 생성 send_data = {'result' : result , 'time' : current_time , 'item' : [] } result_item = { "outcome": result, "id": str(primary_key), # Type : string "prob": int(prob*100) } ## REST-API 보내는 데이터 send_data['item'].append(result_item) ITEM_DICT = {'item' : []} item_info = DF[DF['Item_id'] == primary_key] # SELECT * FROM table WHERE Item_id = primary_key temp_dict = { 'id' : primary_key, 'name' : item_info['Product_name'].values[0], 'price' : int(item_info['Price'].values[0]), 'Qty' : 1 } ITEM_DICT['item'].append(temp_dict) if result : asyncio.get_event_loop().run_until_complete(connect(ITEM_DICT)) # 서버 송신 (분류 성공 >> REST-API , 분류 실패 >> REST-API / S3) response = send_REST_API(send_data) if not result : send_S3(file_name) # 분류 실패 시 S3에 이미지파일도 전송 print(f'서버 송신 결과 : {response}')