def Do(self): try: for case in switch(self.__event): if case('subscribe'): # 关注 print u"粉丝[%s]关注公众账号" % (self.__dict['ToUserName']) self.__dict[ 'Content'] = u'欢迎关注,小猴子已掌握生活百科知识,学会了很多技能,能帮您查天气、菜谱、列车、油价、股票,还会算数、中英互译。能陪您聊天逗乐,简直是居家旅行必备[Smirk]...'.encode( "utf-8") return self.__FormatXmlText() break if case('unsubscribe'): # 取消关注 print u"粉丝[%s]取消关注公众账号" % (self.__dict['ToUserName']) return 'success' break if case('MASSSENDJOBFINISH'): # 群发消息结果 status = self.__xmlData.find('Status').text # 群发的结果 totalCount = self.__xmlData.find( 'TotalCount').text # group_id下粉丝数;或者openid_list中的粉丝数 filterCount = self.__xmlData.find( 'FilterCount' ).text # 过滤(过滤是指,有些用户在微信设置不接收该公众号的消息)后,准备发送的粉丝数,原则上,FilterCount = SentCount + ErrorCount sentCount = self.__xmlData.find( 'SentCount').text # 发送成功的粉丝数 errorCount = self.__xmlData.find( 'ErrorCount').text # 发送失败的粉丝数 print u"群发消息[%s],粉丝总数%s,群发送粉丝%s,成功%s,失败%s" % ( status, totalCount, filterCount, sentCount, errorCount) if case(): return 'success' break except Exception, ex: print "Event.Do error: %s" % (ex) return ''
def __FormatRespond(self, respond): def FormatMsg(msgType, values): return {'MsgType': msgType, 'Values': values[msgType]} def FormatNews(values): L = [] for news in values['news']: if len(news['name']) > 0: L.append({ 'MsgType': 'news', 'Title': news['name'], 'PicUrl': news['icon'], 'Url': news['detailurl'] }) return {'MsgType': 'news', 'Values': L} L = [] for results in respond: resultType = results['resultType'] values = results['values'] for case in switch(resultType): if case('text'): L.append(FormatMsg('text', values)) break if case('url'): L.append(FormatMsg('url', values)) break if case('voice'): L.append(FormatMsg('voice', values)) break if case('video'): L.append(FormatMsg('video', values)) break if case('image'): L.append(FormatMsg('image', values)) break if case('news'): L.append(FormatNews(values)) break if case(): L.append({ 'MsgType': 'text', 'Values': u'抱歉, 小猴子还没学会该项技能,请稍后再试试...' }) break return L
def __format_respond(self, respond): def format_text(values): return values['text'] + '\n' def format_url(values): return values['url'] + '\n' def format_news(values): msg = '' i = 1 for news in values['news']: if len(news['name']) > 0: #msg += i + u'、' + news['info'] + ':\n' + news['name'] + '[' + news['detailurl'] + ']\n' msg += str(i) + '.' + news['name'] + '\n' i += 1 return msg msg = '' for results in respond: resultType = results['resultType'] values = results['values'] for case in switch(resultType): if case('text'): msg += format_text(values) break if case('url'): msg += format_url(values) break if case('news'): msg += format_news(values) break if case('voice'): pass if case('video'): pass if case('image'): pass if case(): msg = u'抱歉, 我的大脑短路了,请稍后再试试...' return msg
def __chat(self, msgType, msg): try: body = '' for case in switch(msgType): if case('text'): # 文本消息 body = self.__FormatRequestText(msg) break if case('image'): # 图片消息 body = self.__FormatRequestImage(msg) break if case('voice'): # 语音消息 body = self.__FormatRequestVoice(msg) break if case(): raise Exception('ERROR') break if len(body) > 0: print "Send to Tuling: %s" % (body) r = requests.post(self.__url, data=body) respond = json.loads(r.text) print "Recvice from Tuling: %s" % (respond) respond = self.__FormatRespond(respond['results']) """ for item in respond: if item['MsgType'] == 'news': for news in item['Values']: print "MsgType:%s, Title:%s, PicUrl:%s, Url:%s" % (news['MsgType'], news['Title'], news['PicUrl'], news['Url']) else: print "MsgType:%s, Values:%s" % (item['MsgType'], item['Values']) """ return respond else: raise Exception('ERROR') except Exception: return [{ 'MsgType': 'text', 'Values': u'抱歉, 小猴子还没学会该项技能,请稍后再试试...' }]
# cv2.imshow('Foreground',thresh) # # cv2.imshow('normal', frame) # k = cv2.waitKey(1) & 0xFF # if k == 27: # break while(success): success, frame = cap.read() img = frame #数据驱使的检测框 if frame_count == frame_cerrent: x1, y1, w1, h1 = track_window_data[0] cv2.rectangle(img, (x1, y1), (x1 + w1, y1 + h1), (255,0,0), 2) for case in tl.switch(data_number): # if case(1): # cv2.imshow('TRACKING', img) # break if case(2): x2, y2, w2, h2 = track_window_data[1] cv2.rectangle(img, (x2, y2), (x2 + w2, y2 + h2), (0,255,0), 2) # cv2.imshow('TRACKING', img) break if case(3): x2, y2, w2, h2 = track_window_data[1] x3, y3, w3, h3 = track_window_data[2] cv2.rectangle(img, (x2, y2), (x2 + w2, y2 + h2), (0,255,0), 2) cv2.rectangle(img, (x3, y3), (x3 + w3, y3 + h3), (0,0,255), 2) # cv2.imshow('TRACKING', img) break
def main(fitter, frames, metadata, q): q.put(fitter.main(frames, metadata)) # %% Main if __name__ == '__main__': pr.enable() ROI_locations = np.load('ROI_locations.npy') ROI_locations = ROI_locations - 1 ROI_locations = ROI_locations[0:8, :] ## switch array columns since MATLAB gives x,y. Python likes y,x ROI_locations = tools.switch(ROI_locations) for name in filenames: with ND2_Reader(name) as ND2: ## parse ND2 info frames = ND2 metadata = ND2.metadata frames = frames[0:20] start = time.time() processes = [] q = mp.Queue() for i in range(0, n_processes): roi_locations_split = np.array_split(ROI_locations, n_processes)
def Do(self): def ImageClassify(mediaId): # 图像识别 mediaIdAndURL = MediaIdAndMediaURL() # 资源文件上传下载 filePath, picUrl = mediaIdAndURL.GetMediaURL( 'image', mediaId) # 通过微信mediaId下载文件,生成新浪云url classify = BaiduImageClassify() classify.ImageClassify(filePath) try: for case in switch(self.__msgReceive.msgType): if case('text'): # 文本消息 msg = self.__msgReceive.content response = self.__tulingRobot.ChatText(msg) break if case('image'): # 图片消息,暂不支持 # 启动异步线程分析图片 new_thread = threading.Thread( target=ImageClassify, args=(self.__msgReceive.mediaId, )) new_thread.start() #filePath, picUrl = mediaIdAndURL.GetMediaURL('image', self.__msgReceive.mediaId) #response = self.__tulingRobot.ChatImage(picUrl) #tuling暂不支持 response = [{'MsgType': 'text', 'Values': u'我看破,我不说。'}] break if case('voice'): # 语音消息,免费版图灵不支持语音,此处调用百度语音进行转换 if len(self.__msgReceive.recognition) > 0: # 使用微信语音识别结果 msg = self.__msgReceive.recognition print "wechatSTT: %s" % (msg) else: # 使用百度语音识别 if self.__msgReceive.format != 'amr': raise Exception('Not support voice format "%s"' % (self.__msgReceive.format)) self.__mediaIdAndURL = MediaIdAndMediaURL() # 资源文件上传下载 filePath, voiceUrl = self.__mediaIdAndURL.GetMediaURL( 'voice', self.__msgReceive.mediaId ) # 通过微信mediaId下载文件,生成新浪云url self.__baiduSTT = BaiduSTT( self.__msgReceive.fromUserName) # 百度语音识别 msg = self.__baiduSTT.STT(filePath) print "baiduSTT: %s" % (msg) response = self.__tulingRobot.ChatText(msg) break if case('location'): # 地理位置消息,图灵暂不支持,此处当成文本方式,只传地理位置信息 msg = u'地址:' + self.__msgReceive.label response = self.__tulingRobot.ChatText(msg) break if case('link'): # 链接消息,图灵暂不支持,此处当成文本方式,只传标题+描述信息 msg = self.__msgReceive.title + self.__msgReceive.description response = self.__tulingRobot.ChatText(msg) break if case('video'): # 视频消息 pass if case('shortvideo'): # 小视频消息 pass if case(): response = self.__tulingRobot.ChatAll( self.__msgReceive.msgType, '') break for item in response: if item['MsgType'] in ['image', 'voice', 'video']: filePath, item[ 'MediaId'] = self.__mediaIdAndURL.GetMediaID( item['MsgType'], item['Values']) return self.__msgReply.PackMsg(response) except Exception, ex: print 'ChatRobot.Chat error: %s' % (ex) return self.__msgReply.PackMsg([{ 'MsgType': 'text', 'Values': u'抱歉, 小猴子还没学会该项技能,请稍后再试试...' }])
def gethosts(con_key, flag): os.remove('/opt/local/pickle/upstreams.pkl') _, upstreams = getinfo.main() cluster = upstreams.get(con_key) cl = consul() hosts = [] for bk in cluster: idc = bk.get('idc') ip = bk.get('ip') port = bk.get('port') lock = bk.get('lock') down = bk.get('down') bid = bk.get('backend_id') # 判断 机器状态,将锁定机器自动解锁 if down == 1 and lock: res = simplejson.loads(cl.onoff('on', bid)) if res.get('code') != 0: logger.error('{}:{} get on failed, {}', format(ip, port, res.get('msg'))) sys.exit(res.get('code')) else: logger.info('{}:{} is online.'.format(ip, port)) down = 0 lock = False for case in switch(flag): if case(r'(((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\.){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9])):\d{2,5}' ): if flag == '{}:{}'.format(ip, port): hosts.append((idc, ip, port, lock, down, bid)) else: continue break if case(r'(((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\.){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9])):\*' ): if flag == '{}:{}'.format(ip, '*'): hosts.append((idc, ip, port, lock, down, bid)) else: continue break if case(r'(((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\.){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9])):!\d{2,5}' ): iflag, pflag = flag.split(':') if iflag == ip and pflag != '!{}'.format(port): hosts.append((idc, ip, port, lock, down, bid)) else: continue break if case(r'!(((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\.){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9])):\d{2,5}' ): iflag, pflag = flag.split(':') if iflag != '!{}'.format(ip) and pflag == str(port): hosts.append((idc, ip, port, lock, down, bid)) else: continue break if case(r'!(((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\.){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9])):\*' ): iflag, pflag = flag.split(':') if iflag != '!{}': hosts.append((idc, ip, port, lock, down, bid)) else: continue break if case(r'!(((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))\.){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9])):!\d{2,5}' ): if flag != '!{}:!{}'.format(ip, port): hosts.append((idc, ip, port, lock, down, bid)) else: continue break if case(r'\*:\d{2,5}'): if flag == '*:{}'.format(port): hosts.append((idc, ip, port, lock, down, bid)) else: continue break if case(r'\*:!\d{2,5}'): if flag != '*:{}'.format(port): hosts.append((idc, ip, port, lock, down, bid)) else: continue if case(r'\*|\*:\*'): hosts.append((idc, ip, port, lock, down, bid)) return hosts
def ParseMsg(self, xmlData): # 解析xml try: self.__xmlData = xmlData self.toUserName = self.__xmlData.find('ToUserName').text # 开发者微信号 self.fromUserName = self.__xmlData.find( 'FromUserName').text # 发送方帐号(一个OpenID) self.createTime = self.__xmlData.find( 'CreateTime').text # 消息创建时间 (整型) self.msgType = self.__xmlData.find('MsgType').text # 消息类型 self.MsgId = self.__xmlData.find('MsgId').text # 消息id,64位整型 for case in switch(self.msgType): if case('text'): # 文本消息 self.content = self.__xmlData.find('Content').text.encode( "utf-8") # 文本消息内容 break if case('image'): # 图片消息 self.mediaId = self.__xmlData.find( 'MediaId').text # 消息媒体id,可以调用多媒体文件下载接口拉取数据 break if case('voice'): # 语音消息 self.mediaId = self.__xmlData.find( 'MediaId').text # 消息媒体id,可以调用多媒体文件下载接口拉取数据 self.format = self.__xmlData.find( 'Format').text # 语音格式,如amr,speex等 self.recognition = self.__xmlData.find('Recognition').text if self.recognition != None: self.recognition = self.recognition.encode( "utf-8") # 语音识别结果,UTF8编码,如开启语音识别则存在 else: self.recognition = '' break if case('video'): # 视频消息 self.mediaId = self.__xmlData.find( 'MediaId').text # 消息媒体id,可以调用多媒体文件下载接口拉取数据 self.thumbMediaId = self.__xmlData.find( 'ThumbMediaId').text # 视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据 break if case('shortvideo'): # 小视频消息 self.mediaId = self.__xmlData.find( 'MediaId').text # 消息媒体id,可以调用多媒体文件下载接口拉取数据 self.thumbMediaId = self.__xmlData.find( 'ThumbMediaId').text # 视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据 break if case('location'): # 地理位置消息 self.location_X = self.__xmlData.find( 'Location_X').text # 地理位置维度 self.location_Y = self.__xmlData.find( 'Location_Y').text # 地理位置经度 self.scale = self.__xmlData.find('Scale').text # 地图缩放大小 self.label = self.__xmlData.find('Label').text # 地理位置信息 break if case('link'): # 链接消息 self.title = self.__xmlData.find('Title').text # 消息标题 self.description = self.__xmlData.find( 'Description').text # 消息描述 self.url = self.__xmlData.find('Url').text # 消息链接 break if case(): pass break except Exception, ex: print "MsgReceive.ParseMsg error: %s" % (ex) return ''