def srvLoadCaliFile(self, params): try: # rector = ImageRector(size=(640,480),cali_file=params.data) # self.camera.rector = rector self.camera.loadCaliFile(params.data) return SetStringResponse('加载成功') except Exception as e: return SetStringResponse('加载失败')
def cbInputChar(self,params): data = params.data if self.process and self.process.isalive(): self.process.sendline(data) # self.process.send(data) return SetStringResponse('输入完成') else: return SetStringResponse('程序未运行')
def cbRemoveFaceLabel(self, params): if len(params.data) == 0: return SetStringResponse("未提供要删除的标签") try: res = self.recognizer.remove_face_label(params.data) return SetStringResponse(res) except Exception as e: print(e) return SetStringResponse("删除失败")
def cbShutdownNode(self,params): rospy.loginfo(params) nodes = rosnode.get_node_names() if params.data in nodes: rosnode.kill_nodes([params.data]) time.sleep(0.5) return SetStringResponse(u'关闭中') else: return SetStringResponse(u'节点未启动')
def srv_delete_ns(self, params): try: data_dir = os.path.join(self.data_root, params.data) if len(data_dir) > len(self.data_root): os.system('rm -rf ' + data_dir) return SetStringResponse("删除成功") except Exception as e: print(e) return SetStringResponse("删除失败")
def srv_delete_cat(self, params): if self.ns is None: return SetStringResponse("请先设置训练名称") try: data_dir = os.path.join(self.data_root, self.ns, params.data) if len(data_dir) > len(self.data_root): os.system('rm -rf ' + data_dir) return SetStringResponse("删除成功") except Exception as e: print(e) return SetStringResponse("删除失败")
def srv_set_ns(self, params): try: data_dir = os.path.join(self.data_root, params.data) if not os.path.exists(data_dir): os.makedirs(data_dir) self.ns = params.data self.ic.ns = params.data return SetStringResponse("设置成功") except Exception as e: print(e) return SetStringResponse("设置失败")
def launchNode(self,node_name): if node_name not in available_nodes: return SetStringResponse(u'不支持该节点') index = available_nodes.index(node_name) try: os.system(launch_cmds2[index]) time.sleep(0.5) return SetStringResponse(u'启动中,请稍等') except Exception as e: rospy.loginfo(e) return SetStringResponse(u'启动出错')
def cbAddFaceLabel(self, params): if len(params.data) == 0: return SetStringResponse("添加失败,名称长度为0") try: cv_image = toImage(self.image_msg) res = self.recognizer.add_face_label(cv_image, params.data, save=True) return SetStringResponse(res) except Exception as e: print(e) return SetStringResponse("添加失败")
def srv_train_classifier(self, params): try: epochs = int(params.data) if epochs <= 0: return SetStringResponse('至少训练1次~') data_dir = os.path.join(self.data_root, self.ns) self.epochs = epochs self.ic.ns = self.ns self.ic.train(data_dir, epochs, self.pub_training_logs) return SetStringResponse('训练完成,可以进行测试了') except Exception as e: print(e) return SetStringResponse('训练失败')
def cbRunProcessBackground(self,params): cmd = params.data try: process = pexpect.spawn(cmd) process.logfile = sys.stdout # self.subprocesses.append(process) self.process = process # while process.isalive() : # time.sleep(1) return SetStringResponse('执行中') except Exception as e: rospy.loginfo(e) return SetStringResponse(e.message)
def srv_create_cat(self, params): if self.ns == None: return SetStringResponse("请先设置训练名称") try: data_dir = os.path.join(self.data_root, self.ns, params.data) if os.path.exists(data_dir): print('data_dir %s exists' % (data_dir)) return SetStringResponse("分类已存在") else: print('create data_dir %s' % (data_dir)) os.makedirs(data_dir) return SetStringResponse("创建成功") except Exception as e: print(e) return SetStringResponse("创建失败")
def cbLaunchNode(self,params): rospy.loginfo(params) nodes = rosnode.get_node_names() if params.data in nodes: return SetStringResponse(u'节点已启动') else: return self.launchNode(params.data)
def srv_set_ns(self, params): try: data_dir = os.path.join(self.ic.data_root, params.data) model_path = os.path.join(data_dir, 'model.h5') label_path = os.path.join(data_dir, 'labelmap.txt') self.ic.ns = params.data if not os.path.exists(data_dir): os.makedirs(data_dir) return SetStringResponse("已创建训练,继续添加分类和数据以训练模型") if os.path.exists(model_path) and os.path.exists(label_path): self.ic.load_label_name() self.ic.load_model() return SetStringResponse("已加载训练,并加载已训练的模型") return SetStringResponse("已加载训练,添加分类和数据训练模型吧") except Exception as e: print(e) return SetStringResponse("设置失败")
def cbTTSOffline(self, params): data = params.data try: cmd = 'bash -c "cd %s && LD_LIBRARY_PATH=./ ./tts_offline %s && aplay ./tts_sample.wav"' % ( self.bin_dir, data) print(cmd) os.system(cmd) except Exception as e: print(e) return SetStringResponse(data)
def srvCameraSaveFrame(self, params): if self.ns is None: return SetStringResponse("训练没有定义,创建或者选择一次训练") try: cat_name = params.data directory = os.path.join(self.data_root, self.ns, cat_name) file_name = '%d.jpg' % (len(os.listdir(directory)) + 1) full_path = os.path.join(directory, file_name) if self.cv_image is not None: # cv_image = self.bridge.imgmsg_to_cv2(self.img_msg, desired_encoding="bgr8") cv2.imwrite(full_path, self.cv_image) # not working # print(self.cv_image) # cv2.imencode('.jpg', self.cv_image)[1].tofile(full_path) # 正确方法 return SetStringResponse("保存至%s成功" % (full_path)) else: return SetStringResponse("保存出错") except Exception as e: print(e) return SetStringResponse("保存出错") finally: pass
def cbToggleHotwordDetect(self, params): if params.data == 'open': if self.detector._running: return SetStringResponse('正在运行') else: self.hotwordDetect = True while not self.detector._running: time.sleep(0.03) return SetStringResponse('已启动') elif params.data == 'close': if self.detector._running: # self.detector.terminate() self.hotwordDetect = False while self.detector._running: time.sleep(0.03) return SetStringResponse('已停止') else: return SetStringResponse('没有运行') else: print(params.data) return SetStringResponse(params.data)
def srvCameraSaveFrame(self, params): if self.ic.ns is None: return SetStringResponse("训练没有定义,创建或者选择一次训练") try: # res = self.get_frame(GetFrameRequest()) # self.image_msg = res.image cat_name = params.data directory = os.path.join(self.ic.data_root, self.ic.ns, cat_name) file_name = '%d.jpg' % (len(os.listdir(directory)) + 1) full_path = os.path.join(directory, file_name) if self.image_msg is not None: cv_image = self.getImage(self.image_msg) xmin, ymin, xmax, ymax = self.getBox() cv_image = cv_image[ymin:ymax, xmin:xmax] cv2.imwrite(full_path, cv_image) # print(cv_image) return SetStringResponse("保存至%s成功" % (full_path)) else: return SetStringResponse("图像信息为空") except Exception as e: print(e) return SetStringResponse("保存出错") finally: pass