def __init__(self, s, count): threading.Thread.__init__(self) self.cs = s self.gui = Server_GUI.GUI() Server_GUI.iscreate2 = True if count == 0: # initialize first position of every thing in the room vcap = cv2.VideoCapture(camera_port) self.rret, self.rframe = vcap.read() cv2.imwrite('init_recog.jpg', self.rframe) self.cli = client.client_socket() init = threading.Thread(target=self.cli.create, args=('init_recog.jpg', True)) init.start() # initialize the coordinate cv2.namedWindow('image') cv2.setMouseCallback('image', self.draw_circle) self.img = cv2.imread('tmpdemo.jpg') while (1): cv2.imshow('image', self.rframe) # cv2.imshow('image', self.img) k = cv2.waitKey(1) if co == 3: cv2.destroyAllWindows() break cv2.imwrite('init_coor.jpg', self.rframe) init.join() print('initial object recognition: ', before_pos, '\n')
import client import OpenCV color = client.client_socket() print("The Choosen color was ", color) print("Press Esc to quit...") OpenCV.img_process(color)
def run(self): while True: self.recv_msg = self.cs.recv(1024) if not self.recv_msg: break else: if self.validCommand(self.recv_msg.decode('utf-8')) > 0: if self.validCommand(self.recv_msg.decode('utf-8')) != 3: self.cs.send('正在進行辨識\n'.encode('utf-8')) else: self.cs.send('正在紀錄傢俱位置\n'.encode('utf-8')) if os.path.isfile('./output.jpg'): os.system('rm output.jpg') cap = cv2.VideoCapture(camera_port) if not cap.isOpened(): cap.open() # save the 70th frame (for brightness of the image) frameindex = 0 while True: ret, frame = cap.read() # cv2.imshow('frame', frame) if frameindex == 70: cv2.imwrite('output.jpg', frame) break frameindex += 1 cap.release() cv2.destroyAllWindows() # receiving result from workstation server self.cli = client.client_socket() rec_result = self.cli.create('output.jpg', False) result_list = rec_result.split() # result back to app if len(result_list) < 3: self.cs.send('無辨識結果\n'.encode('utf-8')) else: if self.validCommand( self.recv_msg.decode('utf-8')) == 1: # 有沒有移動過 if self.isMoved(self.obj, before_pos, result_list) == 1: self.cs.send('沒有'.encode('utf-8')) self.cs.send( self.translateEtoC( self.obj).encode('utf-8')) self.cs.send('的辨識結果\n'.encode('utf-8')) elif self.isMoved(self.obj, before_pos, result_list) == 2: self.cs.send( self.translateEtoC( self.obj).encode('utf-8')) self.cs.send('有移動過\n'.encode('utf-8')) else: self.cs.send( self.translateEtoC( self.obj).encode('utf-8')) self.cs.send('沒有移動過\n'.encode('utf-8')) elif self.validCommand( self.recv_msg.decode('utf-8')) == 2: # 在哪裡 for index, rl in enumerate(result_list): # print(rl, self.obj) if rl == self.obj: objdir = self.direction( result_list[index + 1], result_list[index + 2]) origin = self.four( 320, 480 ) # calculate coordinate of (320,480) realxy = self.four( int(float(result_list[index + 1])), int(float(result_list[index + 2]))) objdis = self.distance( realxy[0], realxy[1], origin[0], origin[1]) step = math.ceil(objdis / 4) # sample:椅子大約在你的左前方五步 並在導盲杖可觸及的範圍內 self.cs.send( self.translateEtoC( self.obj).encode('utf-8')) self.cs.send('大約在你的'.encode('utf-8')) self.cs.send(objdir.encode('utf-8')) self.cs.send(str(step).encode('utf-8')) self.cs.send( '步,並在導盲杖可觸及的範圍內\n'.encode('utf-8')) break elif index == len(result_list) - 1: self.cs.send('沒有'.encode('utf-8')) self.cs.send( self.translateEtoC( self.obj).encode('utf-8')) self.cs.send('的辨識結果\n'.encode('utf-8')) elif self.validCommand( self.recv_msg.decode('utf-8')) == 3: # 紀錄家具位置 self.cs.send('傢俱位置紀錄完畢\n'.encode('utf-8')) elif self.validCommand( self.recv_msg.decode('utf-8')) == 4: # 我想找XX print(result_list) if not 'diningtable' in result_list or not 'person' in result_list: self.cs.send( '沒有桌子的辨識結果或無法定位您的位置\n'.encode('utf-8')) else: if not self.obj in result_list: self.cs.send('沒有'.encode('utf-8')) self.cs.send( self.translateEtoC( self.obj).encode('utf-8')) self.cs.send('的辨識結果\n'.encode('utf-8')) else: # person and table detected ''' (If person is at the other side of the table, change the direction.) | p | (faceing down) ----------------- | 1 | 2 | 3 | 4 | +---+---+---+---+ | 5 | 6 | 7 | 8 | ----------------- ''' table_index = result_list.index( 'diningtable') person_index = result_list.index('person') obj_index = result_list.index(self.obj) table_x = int( float(result_list[table_index + 1])) table_y = int( float(result_list[table_index + 2])) person_y = int( float(result_list[person_index + 2])) obj_x = int( float(result_list[obj_index + 1])) obj_y = int( float(result_list[obj_index + 2])) w = int(float(result_list[table_index + 3])) h = int(float(result_list[table_index + 4])) if table_y >= person_y: # person is in back of the table (facing the camera) if obj_y >= table_y - h and obj_y <= table_y - h / 2 and obj_x >= table_x - w and obj_x <= table_x + w: # object is between person and up half of the table on the image if obj_x >= table_x + w / 2 and obj_x <= table_x + w: # object is at position 4 self.cs.send( self.translateEtoC( self.obj).encode( 'utf-8')) self.cs.send( '在你面對桌子上的左手邊\n'.encode( 'utf-8')) elif obj_x <= table_x - w / 2 and obj_x >= table_x - w: # object is at position 1 self.cs.send( self.translateEtoC( self.obj).encode( 'utf-8')) self.cs.send( '在你面對桌子上的右手邊\n'.encode( 'utf-8')) else: # object is at position 2 or 3 self.cs.send( self.translateEtoC( self.obj).encode( 'utf-8')) self.cs.send( '在你面對桌子上的前方\n'.encode( 'utf-8')) elif obj_y >= table_y - h / 2 and obj_y <= table_y and obj_x >= table_x - w and obj_x <= table_x + w: # object is at down half of the table on the image if obj_x >= table_x + w / 2 and obj_x <= table_x + w: # object is at position 8 self.cs.send( self.translateEtoC( self.obj).encode( 'utf-8')) self.cs.send( '在你面對桌子上的左前方\n'.encode( 'utf-8')) elif obj_x <= table_x - w / 2 and obj_x >= table_x - w: # object is at position 5 self.cs.send( self.translateEtoC( self.obj).encode( 'utf-8')) self.cs.send( '在你面對桌子上的右前方\n'.encode( 'utf-8')) else: # object is at position 2 or 3 self.cs.send( self.translateEtoC( self.obj).encode( 'utf-8')) self.cs.send( '在你面對桌子上的前方\n'.encode( 'utf-8')) else: # object is not in the range of table self.cs.send( self.translateEtoC( self.obj).encode('utf-8')) self.cs.send( '不在桌子上,請使用另一指令\n'.encode( 'utf-8')) else: # person is in front of the table (not facing the camera) if obj_y >= table_y - h / 2 and obj_y <= table_y and obj_x >= table_x - w and obj_x <= table_x + w: if obj_x >= table_x + w / 2 and obj_x <= table_x + w: # position 8 self.cs.send( self.translateEtoC( self.obj).encode( 'utf-8')) self.cs.send( '在你面對桌子上的右手邊\n'.encode( 'utf-8')) elif obj_x <= table_x - w / 2 and obj_x >= table_x - w: # position 5 self.cs.send( self.translateEtoC( self.obj).encode( 'utf-8')) self.cs.send( '在你面對桌子上的左手邊\n'.encode( 'utf-8')) else: self.cs.send( self.translateEtoC( self.obj).encode( 'utf-8')) self.cs.send( '在你面對桌子上的前方\n'.encode( 'utf-8')) elif obj_y >= table_y - h and obj_y <= table_y - h / 2 and obj_x >= table_x - w and obj_x <= table_x + w: if obj_x >= table_x + w / 2 and obj_x <= table_x + w: # position 4 self.cs.send( self.translateEtoC( self.obj).encode( 'utf-8')) self.cs.send( '在你面對桌子上的右前方\n'.encode( 'utf-8')) elif obj_x <= table_x - w / 2 and obj_x >= table_x - w: # position 1 self.cs.send( self.translateEtoC( self.obj).encode( 'utf-8')) self.cs.send( '在你面對桌子上的左前方\n'.encode( 'utf-8')) else: self.cs.send( self.translateEtoC( self.obj).encode( 'utf-8')) self.cs.send( '在你面對桌子上的前方\n'.encode( 'utf-8')) else: self.cs.send( self.translateEtoC( self.obj).encode('utf-8')) self.cs.send( '不在桌子上,請使用另一指令\n'.encode( 'utf-8')) # update before_pos[] rl = 0 while rl + 3 <= len(result_list): same = False for bp in before_pos: # every obj in result_list is compared with whole before_pos list if result_list[rl] == bp[0]: same = True bp[1] = result_list[rl + 1] bp[2] = result_list[rl + 2] if not same: # new detected object tmp = [] tmp.append(result_list[rl]) tmp.append(result_list[rl + 1]) tmp.append(result_list[rl + 2]) before_pos.append(tmp) if result_list[rl] == 'diningtable': # skip h and w rl += 2 rl += 3 print('updated object: ', before_pos, '\n') else: send_msg = '指令錯誤 請重新輸入\n' self.cs.send(send_msg.encode('utf-8'))
def test_dir_request(): """Test if a valid request for a dir gets return a proper HTML listing.""" listing = ['JPEG_example.jpg', 'Sample_Scene_Balls.jpg', 'sample_1.png'] returned = client_socket('GET images/ HTTP/1.1 Host:') ok_message = response_ok((listing, 3, 'HTML LISTING')) assert returned == ok_message
def test_file_request(): """Test if a valid request for file gets return properly.""" txt = 'This is a very simple text file. Just to show that we can serve it up. It is three lines long.' returned = client_socket('GET sample.txt HTTP/1.1 Host:') ok_message = response_ok((txt, 95, '.txt')) assert returned[:50] == ok_message[:50]
def test_400_error_message_respoonse(): """Testing if 500 error is triggered.""" returned = client_socket('GET /URI HTTP/1.1 HO') error_message = response_error('400') assert returned == error_message
def test_505_error_message_response(): """Testing if 505 error is triggered.""" returned = client_socket('GET /URI HTTP/1.2 Host:') error_message = response_error('505') assert returned == error_message
def test_404_message_response(): """Testing for if 200 ok is triggered.""" returned = client_socket('GET /URI HTTP/1.1 Host:') error_message = response_error('404') assert returned == error_message
def test_405_error_message_response(): """Testing for if 405 error is triggered.""" returned = client_socket('POST /URI HTTP/1.1 Host:') error_message = response_error('405') assert returned == error_message