def get_episode_loader(img_size, nc, ns, nq): frames_dir = '../../dataset/OptFace/tracked_frames' _, img_map, reverse_map, _, _ = get_frames_info() episode_loader = Function.get_episode_lodaer(frames_dir, reverse_map, img_map, img_size, nc, ns, nq) return episode_loader
def detect_face(self, img_np, nms_thresh=0.3, thresh=0.8): img_nd = Function.np2nd(img_np, self.ctx) self.arg_params["data"] = img_nd im_info = mx.ndarray.array([[img_nd.shape[2], img_nd.shape[3], 1]], ctx=self.ctx) self.arg_params["im_info"] = im_info exe = self.sym.bind(self.ctx, self.arg_params, args_grad=None, grad_req="null", aux_states=self.aux_params) exe.forward(is_train=False) output_dict = {name: nd for name, nd in zip(self.sym.list_outputs(), exe.outputs)} rois = output_dict['rpn_rois_output'].asnumpy()[:, 1:] # first column is index scores = output_dict['cls_prob_reshape_output'].asnumpy()[0] bbox_deltas = output_dict['bbox_pred_reshape_output'].asnumpy()[0] pred_boxes = bbox_pred(rois, bbox_deltas) pred_boxes = clip_boxes(pred_boxes, (img_nd.shape[2],\ img_nd.shape[3])) cls_boxes = pred_boxes[:, 4:8] cls_scores = scores[:, 1] keep = np.where(cls_scores >= thresh)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets.astype(np.float32), nms_thresh) dets = dets[keep, :] dets[:, :4] = (dets[:, :4]).round().astype(np.int32) return dets
def SetMusic(UserId, MusicId, Level, BaseRate): Music = Func.Get_BestScore(UserId, MusicId) DataBase = DB.LoadBaseRate() Dic = { 'MusicId': MusicId, 'Level': Level, 'MusicName': Music['musicName'], 'Image': Music['musicFileName'], 'ArtistName': Music['artistName'], 'BaseRate': BaseRate } DataBase.SetMusic(Dic)
def initializeFromDB(self,userid): ### Get all existing time card data and store it in an array. self.cardData = s.searchCards('admin',Settings.users['admin'],userid) ### Check the date. If the latest card is out of date make cards until the latest card is up to date. currentDate = datetime.datetime.now() periods = Function.getPeriodStart( Function.getYear(), Settings.startDay ) ### itterate over periods for period in periods: if period.month < currentDate.month or (period.day <= currentDate.day and period.month == currentDate.month): if not self.checkCard(period): #see if period is already in cardData data = { 'StartDate':period.strftime(Settings.timeformat) } s.createTimeCard('admin',Settings.users['admin'],userid,data) self.cardData = s.searchCards('admin',Settings.users['admin'],userid) ### unpack data for day in Settings.days: for data in self.cardData: data[day] = ClockDay(data[day]) return
def compress_video(video_dir, des_dir, ctx_id=0, video_id=0): """ Extracted the face-involved frames and output to des_dir """ ctx = mx.gpu(ctx_id) video_capture = cv2.VideoCapture(video_dir) fps = video_capture.get(cv2.CAP_PROP_FPS) size = (int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)), \ int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) fourcc = cv2.VideoWriter.fourcc('D', 'I', 'V', 'X') video_writer = cv2.VideoWriter(des_dir, fourcc, int(fps), size) video_capture.get(cv2.CAP_PROP_FRAME_COUNT) num_frame = 0 model_name = '../detection/mxnet-face-fr50' _, arg_params, aux_params = mx.model.load_checkpoint(model_name, 0) arg_params, aux_params = Function.chg_ctx(arg_params, aux_params, ctx) sym = faster_rcnn.faster_rcnn50(num_class=2) buffer_size = 50 frame_buffer = queue.Queue(buffer_size) record_mode = False missed_frames_cnt = 0 while video_capture.grab(): flag, frame = video_capture.retrieve() frame_nd = F.np2nd(frame, ctx=ctx) if not flag: continue num_frame += 1 if num_frame % 500 == 0: print('Video id: %d Num frame: %d\n' % (video_id, num_frame)) # put the current frame into a buffer frame_buffer.put(frame) boxes = detect_face(ctx, sym, arg_params, aux_params, frame_nd) if len(boxes) > 0: if not record_mode: record_mode = True dump_frames(video_writer, frame_buffer) elif frame_buffer.full(): dump_frames(video_writer, frame_buffer) elif record_mode: missed_frames_cnt += 1 if frame_buffer.full(): dump_frames(video_writer, frame_buffer) if missed_frames_cnt > buffer_size: dump_frames(video_writer, frame_buffer) record_mode = False missed_frames_cnt = 0 if frame_buffer.full(): frame_buffer.get()
def CheckMusic(userId): MusicIdList = Func.Get_MusicIdList(userId) DataBase = DB.LoadBaseRate() BaseRateList = DataBase.Get_BaseRateList() NoneMusicList = [] ExistMusicList = [] for level in range(2, 4): for MusicId in MusicIdList[level - 2]: if MusicId in BaseRateList[level - 2]: Music = DataBase.Get_BaseRate(MusicId, level) if Music['BaseRate'] is not None: BaseRate = Music['BaseRate'] Dic = { 'MusicId': MusicId, 'MusicName': Music['MusicName'], 'MusicImage': Music['Image'], 'ArtistName': Music['ArtistName'], 'Level': level, 'BaseRate': BaseRate, 'AirPlus': Music['AirPlus'] } ExistMusicList.append(Dic) continue Music = Func.Get_BestScore(userId, MusicId) Dic = { 'MusicId': MusicId, 'MusicName': Music['musicName'], 'MusicImage': Music['musicFileName'], 'ArtistName': Music['artistName'], 'Level': level, 'BaseRate': None, 'AirPlus': False } NoneMusicList.append(Dic) DataBase.SetMusic(Dic, True) return NoneMusicList, ExistMusicList
def LoadBest(self): self.cur.execute("SELECT * FROM Best") rows = self.cur.fetchall() if rows: Best = [] dif = {3: 'master', 2: 'expert'} for row in rows: Dic = { 'MusicId': row[0], 'Level': row[1], 'MusicName': row[2], 'Image': row[3], 'BaseRate': row[4], 'Score': row[5], 'MaxScore': row[6], 'Rate': row[7], 'Rank': Func.Score2Rank(row[5]), 'LevelName': dif[row[1]], 'Diff': Func.BaseRate2Diff(row[4]) } Best.append(Dic) return Best else: return None
def extract_tracked_frames(): gpu_id = 1 ctx = mx.gpu(gpu_id) video_path = '../../dataset/OptFace/CompressedVideo' detector = Detector('../../model/faster_rcnn/mxnet-face-fr50', ctx) img_id_map = {} img_cnt = 0 cls_cnt = 0 for video_file in os.listdir(video_path): frames_dir = os.path.join('../../dataset/OptFace/tracked_frames', video_file) if not os.path.exists(frames_dir): os.mkdir(frames_dir) print('processing video: %s' % video_file) video_dir = os.path.join(video_path, video_file) video_capture = cv2.VideoCapture(video_dir) fps = video_capture.get(cv2.CAP_PROP_FPS) size = (int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)), \ int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) num_frame = 0 tracker = sort.Sort() while video_capture.grab(): flag, frame = video_capture.retrieve() det = detector.detect_face(frame) ids = tracker.update(det) # if ids.shape[0] > 0: # print(ids) sticked_img, crops = Function.stick_boxes(frame, ids) for i in range(ids.shape[0]): cur_id = int(ids[i][4]) if img_id_map.get(cur_id) is None: img_id_map[cur_id] = cls_cnt cls_cnt += 1 dump_crop(frames_dir, crops[i], img_id_map[cur_id], img_cnt) img_cnt += 1 """
def LoadRecent(self): self.cur.execute("SELECT * FROM Recent") rows = self.cur.fetchall() if rows: Recent = [] dif = {3: 'master', 2: 'expert'} for row in rows: Dic = { 'MusicId': row[0], 'Level': row[1], 'MusicName': row[2], 'Image': row[3], 'BaseRate': row[4], 'Score': row[5], 'Rate': row[6], 'PlayDate': row[7], 'Rank': Func.Score2Rank(row[5]), 'LevelName': dif[row[1]] } Recent.append(Dic) return Recent else: return None
def SearchMusic(UserId, Dic): DataBase = DB.LoadBaseRate() MusicList = DataBase.SerchMusic_DB(Dic) MusicIdList = { x['MusicId']: idx for idx, x in enumerate(MusicList) if x['Level'] == 2 }, { x['MusicId']: idx for idx, x in enumerate(MusicList) if x['Level'] == 3 } GenreList = Func.Get_Genre(UserId, Dic['Genre'], Dic['DiffLevel']) ResultList = [] if Dic['DiffLevel']: for MusicId in GenreList: if MusicId in MusicIdList[int(Dic['DiffLevel']) - 2]: idx = MusicIdList[int(Dic['DiffLevel']) - 2][MusicId] ResultList.append(MusicList[idx]) else: for level in range(2, 4): for MusicId in GenreList[level - 2]: if MusicId in MusicIdList[level - 2]: idx = MusicIdList[level - 2][MusicId] ResultList.append(MusicList[idx]) return ResultList
def CalcRate(userId): '''レートを計算してデータベースに保存する''' Base = DB.LoadBaseRate() FriendCode = Func.Get_FriendCode(userId) if FriendCode is None: return None Hash = hashlib.sha256(str(FriendCode).encode('utf8')).hexdigest() Rating = {} DataBase = DB.UserDataBase(Hash) #Best枠について MusicIdList = Func.Get_MusicIdList(userId) #MusicIdのリストの取得 if MusicIdList is None: return None Musics = [] i = 0 for Level in range(2, 4): MusicBestScore = Func.Get_DiffList( userId, "1990" + str(Level)) #エキスパート(19902)とマスター(19903)の曲別最大スコアのリストの取得 if MusicBestScore is None: return None for MusicId in MusicIdList[Level - 2]: for Music in MusicBestScore['userMusicList']: if Music['musicId'] == MusicId: MusicDetail = Base.Get_BaseRate(MusicId, Level) if MusicDetail is None or MusicDetail['BaseRate'] is None: continue else: Dic = { 'MusicId': MusicId, 'Level': Level, 'MusicName': MusicDetail['MusicName'], 'Image': MusicDetail['Image'], 'BaseRate': MusicDetail['BaseRate'], 'Rate': Func.Score2Rate(Music['scoreMax'], MusicDetail['BaseRate']), 'Score': Music['scoreMax'] } Musics.append(Dic) #ソート Best = sorted(Musics, key=lambda x: x["Rate"], reverse=True) Rate = {'BestRate': 0, 'MaxBestRate': 0} for Music in Best: if i < 30: Music['MaxScore'] = None Rate['BestRate'] += Music['Rate'] if i == 0: Rate['MaxBestRate'] = Music['Rate'] elif i == 29: Rate['MinBestRate'] = Music['Rate'] else: if Music['Score'] >= 1007500: Music['MaxScore'] = None else: MaxScore = Func.Rate2Score(Music['BaseRate'], Rate['MinBestRate']) if MaxScore <= 1007500 and MaxScore > 0 and MaxScore - Music[ 'Score'] > 0: Music['MaxScore'] = MaxScore else: Music['MaxScore'] = None i += 1 #データーベースに保存 DataBase.SetBest(Best) #Recent Playlog = Func.Get_PlayLog(userId) if Playlog is None: return None Recent = DataBase.LoadRecent() LevelMap = {'master': 3, "expert": 2} FinalPlayDate = Playlog['userPlaylogList'][0]['userPlayDate'][0:-2] Musics = [] for Play in Playlog['userPlaylogList'][0:30]: if Play['levelName'] == 'expert' or Play['levelName'] == 'master': MusicId = Base.Get_MusicId(Play['musicFileName']) if MusicId is None: continue MusicDetail = Base.Get_BaseRate(MusicId, LevelMap[Play['levelName']]) if MusicDetail is None or MusicDetail['BaseRate'] is None: continue else: Dic = { 'MusicId': MusicId, 'Level': LevelMap[Play['levelName']], 'MusicName': MusicDetail['MusicName'], 'Image': MusicDetail['Image'], 'BaseRate': MusicDetail['BaseRate'], 'Rate': Func.Score2Rate(Play['score'], MusicDetail['BaseRate']), 'Score': Play['score'], 'PlayDate': Play['userPlayDate'][0:-2] } Musics.append(Dic) if Recent is None: #レート順にソート Recent = sorted(Musics, key=lambda x: x['Rate'], reverse=True) else: #レート順にソート Recent = sorted(Recent, key=lambda x: x['Rate'], reverse=True) if len(Recent) > 10: UserData = DataBase.LoadUser() #UserDataがゼロではなかったら if len(UserData): OldDate = datetime.strptime(UserData[-1]['FinalPlayDate'], '%Y-%m-%d %H:%M:%S') for Play in Musics: NowDate = datetime.strptime(Play['PlayDate'], '%Y-%m-%d %H:%M:%S') #最後に実行されたときの曲と現在の曲の新旧 if NowDate > OldDate: #Recent枠の最小と比較 if Play['Rate'] > Recent[9]['Rate']: #Recent枠の最小と入れ替え Recent[-1]['MusicId'] = Play['MusicId'] Recent[-1]['Level'] = Play['Level'] Recent[-1]['MusicName'] = Play['MusicName'] Recent[-1]['Image'] = Play['Image'] Recent[-1]['BaseRate'] = Play['BaseRate'] Recent[-1]['Score'] = Play['Score'] Recent[-1]['Rate'] = Play['Rate'] Recent[-1]['PlayDate'] = Play['PlayDate'] elif Play['Score'] >= 1007500: pass elif Play['Score'] >= Recent[-1]['Score']: pass else: #プレイ日時順にソート Recent = sorted( Recent, key=lambda x: datetime.strptime( x['PlayDate'], '%Y-%m-%d %H:%M:%S'), reverse=True) #Recent候補枠の一番古い曲と入れ替え Recent[-1]['MusicId'] = Play['MusicId'] Recent[-1]['Level'] = Play['Level'] Recent[-1]['MusicName'] = Play['MusicName'] Recent[-1]['Image'] = Play['Image'] Recent[-1]['BaseRate'] = Play['BaseRate'] Recent[-1]['Score'] = Play['Score'] Recent[-1]['Rate'] = Play['Rate'] Recent[-1]['PlayDate'] = Play['PlayDate'] #レート順にソート Recent = sorted(Recent, key=lambda x: x['Rate'], reverse=True) else: pass else: pass else: pass RecentRates = 0 i = 0 for Music in Recent: if i < 10: RecentRates += Music['Rate'] i += 1 #ユーザーデータ UserInfo = Func.Get_UserData(userId) if UserInfo is None: return None else: UserInfo = UserInfo['userInfo'] NowDate = datetime.now().strftime('%Y-%m-%d %H:%M:%S') User = { 'TotalPoint': UserInfo['totalPoint'], 'TrophyType': UserInfo['trophyType'], 'WebLimitDate': UserInfo['webLimitDate'][0:-2], 'CharacterFileName': UserInfo['characterFileName'], 'FriendCount': UserInfo['friendCount'], 'Point': UserInfo['point'], 'PlayCount': UserInfo['playCount'], 'CharacterLevel': UserInfo['characterLevel'], 'TrophyName': UserInfo['trophyName'], 'ReincarnationNum': UserInfo['reincarnationNum'], 'UserName': UserInfo['userName'], 'Level': UserInfo['level'], 'FriendCode': FriendCode, 'Hash': Hash, 'FinalPlayDate': FinalPlayDate, 'ExecuteDate': NowDate } #データベースに保存 DataBase.SetRecent(Recent) #レート計算 DispRate = (UserInfo['playerRating'] / 100.0) BestRate = math.floor((Rate['BestRate'] / 30) * 100) / 100 Rating = { 'DispRate': DispRate, 'HighestRating': (UserInfo['highestRating'] / 100.0), 'MaxRate': (math.floor( ((Rate['BestRate'] + Rate['MaxBestRate'] * 10) / 40) * 100) / 100), 'BestRate': BestRate, #'RecentRate':(math.floor(((DispRate * 40 - BestRate * 30) / 10) * 100) / 100), 'RecentRate': (math.floor((RecentRates / 10) * 100) / 100), 'Credits': UserInfo['playCount'], 'ExecuteDate': NowDate } #データベースに保存 DataBase.SetRate(Rating) #データベースに保存 DataBase.SetUser(User) Admin = DB.AdminDataBase() Data = { 'UserName': UserInfo['userName'], 'FriendCode': FriendCode, 'Hash': Hash, 'Credits': UserInfo['playCount'], 'DispRate': DispRate, 'HighestRating': Rating['HighestRating'], 'MaxRate': Rating['MaxRate'], 'BestRate': BestRate, 'RecentRate': Rating['RecentRate'], } Admin.SetData(Data) return Hash
rhsexpr = (144. / a / a) * sin(6. * x / a) solnexpr = 4. * sin(6. * x / a) if ndims == 2: x = xs[0] y = xs[1] rhsexpr = (80. / a / a) * sin(2. * x / a) * sin(4. * y / a) solnexpr = 4. * sin(2. * x / a) * sin(4. * y / a) if ndims == 3: x = xs[0] y = xs[1] z = xs[2] rhsexpr = (224. / a / a) * sin(2. * x / a) * sin(4. * y / a) * sin( 6. * z / a) solnexpr = 4. * sin(2. * x / a) * sin(4. * y / a) * sin(6. * z / a) soln = Function(l2, name='soln') solnproj = Projector(solnexpr, soln, bcs=[]) # ,options_prefix= 'masssys_' solnproj.project() # Create forms and problem u = TestFunction(l2) v = TrialFunction(l2) x = Function(l2, name='x') n = FacetNormal(mesh) # THIS ASSUMES A UNIFORM GRID, SHOULD BE MORE CLEVER... ddx = 1. / nx if variant == 'mgd': penalty = 1. * (1. + 1.) / ddx else: penalty = order * (order + 1.) / ddx
nxs = [nx, ny, nz] PETSc.Sys.Print(variant, velocityspace, order, cell, coordorder, xbcs, nxs) # create mesh and spaces mesh = create_box_mesh(cell, nxs, xbcs, coordorder) elemdict = create_complex(cell, velocityspace, variant, order) adjust_coordinates(mesh, c) l2 = FunctionSpace(mesh, elemdict['l2']) hdiv = FunctionSpace(mesh, elemdict['hdiv']) mixedspace = MixedFunctionSpace([l2, hdiv]) hhat, uhat = TestFunctions(mixedspace) xhat = TestFunction(mixedspace) x = Function(mixedspace, name='x') h, u = split(x) # set boundary conditions fullbcs = [ DirichletBC(mixedspace.sub(1), 0.0, "on_boundary"), ] ubcs = [ DirichletBC(hdiv, 0.0, "on_boundary"), ] if cell in ['tpquad', 'tphex', 'tptri']: fullbcs.append(DirichletBC(mixedspace.sub(1), 0.0, "top")) fullbcs.append(DirichletBC(mixedspace.sub(1), 0.0, "bottom")) ubcs.append(DirichletBC(hdiv, 0.0, "top")) ubcs.append(DirichletBC(hdiv, 0.0, "bottom"))
elemdict = create_complex(cell, 'rt', variant, order) adjust_coordinates(mesh, c) h1 = FunctionSpace(mesh, elemdict['h1']) hhat = TestFunction(h1) h = TrialFunction(h1) # set boundary conditions bcs = [DirichletBC(h1, 0.0, "on_boundary"), ] if cell in ['tpquad', 'tphex', 'tptri']: bcs.append(DirichletBC(h1, 0.0, "top")) bcs.append(DirichletBC(h1, 0.0, "bottom")) # Create forms and problem x = Function(h1, name='x') R = (-hhat * lambdaa * exp(x) + inner(grad(hhat), grad(x))) * dx # degree=(order*2+1) # J = (-hhat * lambdaa * exp(x) * h + inner(grad(hhat), grad(h))) * dx # (degree=(order*2+1)) J = derivative(R, x) # create solvers if mgd_lowest: from mgd_helpers import lower_form_order Jp = lower_form_order(J) else: Jp = J problem = NonlinearVariationalProblem(R, x, J=J, Jp=Jp, bcs=bcs) solver = NonlinearVariationalSolver(problem, options_prefix='nonlinsys_') # solve system
def prepare(code): lines = filter(lambda line: not re.match(r'^\s*//.*', line), code.split('\n')) lines = re.sub(r'\s+', ' ', "".join(lines)).strip().split(';') lines = filter(lambda line: not re.match(r'^\s*$', line), lines) return [Function.parse(line) for line in lines]
def clockNow(self): self.Times.append(Function.getTime())
PETSc.Sys.Print(variant, order, cell, coordorder, xbcs, nxs) nquadplot_default = order if variant == 'mgd' and order > 1: nquadplot_default = 2 nquadplot = OptDB.getInt('nquadplot', nquadplot_default) # create mesh and spaces mesh = create_box_mesh(cell, nxs, xbcs, coordorder) elemdict = create_complex(cell, 'rt', variant, order) adjust_coordinates(mesh, c) h1 = FunctionSpace(mesh, elemdict['h1']) hhat = TestFunction(h1) h = Function(h1, name='h') # set boundary conditions bcs = [DirichletBC(h1, 0.0, "on_boundary"), ] if cell in ['tpquad', 'tphex', 'tptri']: bcs.append(DirichletBC(h1, 0.0, "top")) bcs.append(DirichletBC(h1, 0.0, "bottom")) # set rhs/soln xs = SpatialCoordinate(mesh) a = 1. / pi scale = 1 if ndims == 1: x = xs[0]
def checkCard(self,period): for card in self.cardData: timeData = Function.parseTime(card['StartDate']) if timeData.day == period.day and timeData.month == period.month: return True return False
recognizer.load_train_data(train_data, label, reverse_cls_map) img_id_map = {} img_cnt = 0 cls_cnt = 0 video_list = [l.strip() for l in open(video_list)] for video_file in video_list[0:1]: print('processing video: %s' % video_file) video_dir = os.path.join(video_path, video_file) video_capture = cv2.VideoCapture(video_dir) fps = video_capture.get(cv2.CAP_PROP_FPS) size = (int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)), \ int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) num_frame = 0 tracker = sort.Sort() while video_capture.grab(): flag, frame = video_capture.retrieve() det = detector.detect_face(frame) ids = tracker.update(det) sticked_img, crops = Function.stick_boxes(frame, ids) for crop in crops: crop_name = recognizer.predict(crop) print('crop name: %s' % (crop_name)) # sys.exit() cv2.imshow('a', sticked_img) if cv2.waitKey(5) == ord('q'): break