def download_userobject_urls(self, userobject, target_object, DIR=DIRusers): screen_name = userobject.screen_name if target_object is None: target_object = operate_sql.BotProfile(screen_name) USERDIR = '/'.join([DIR, screen_name]) if not os.path.exists(USERDIR): os.mkdir(USERDIR) target_object.name = userobject.name.replace(' ', '') target_object.description = userobject.description target_object.abs_icon_filename = '' target_object.abs_background_filename = '' target_object.abs_banner_filename = '' try: # if not userobject.profile_image_url is None: if hasattr(userobject, 'profile_image_url'): target_object.abs_icon_filename = _.saveImg( media_url=userobject.profile_image_url.replace( '_normal', ''), DIR=USERDIR, filename=''.join([screen_name, '_icon.jpg'])) except Exception as e: _.log_err() try: # if not userobject.profile_banner_url is None: if hasattr(userobject, 'profile_banner_url'): target_object.abs_banner_filename = _.saveImg( media_url=userobject.profile_banner_url, DIR=USERDIR, filename=''.join([screen_name, '_banner.jpg'])) except Exception as e: _.log_err() return target_object
def make_qrcode(data = 'hello', owner = None): try: qr = qrcode.QRCode(error_correction = qrcode.constants.ERROR_CORRECT_L) qr.add_data(data) qr.make(fit=True) cvimg = qr.make_image() filepath = '/'.join([_.get_thispath(), 'qr.png']) cvimg.save(filepath) _id = operate_sql.file2db(filepath = filepath, filename = None, _format = None, owner = owner) return _id, filepath except Exception as e: _.log_err() return ''
def recognize_faceimage(_id = '7aa33bfe-e6c0-4156-a4d0-7e53e88b1dd1', is_show = True, cascade_lib = cascade_lib_anime, frame_setting = None): result_dic = {} def extract_face(faces, i): #(四角の左上のx座標, 四角の左上のy座標, 四角の横の長さ, 四角の縦の長さ) pos = faces[i] bottom = pos[1]+pos[3] right = pos[0]+pos[2] if not (pos[1] < 0 or right > height or pos[0] < 0 or bottom > width): cvimg = original_cvimg result_dic['extracted'] = {} result_dic['extracted'][i] = {} pos_dic = {} pos_dic['left'] = int(pos[0]) pos_dic['top'] = int(pos[1]) pos_dic['bottom'] = int(bottom) pos_dic['right'] = int(right) pos_dic['height'] = int(pos[2]) pos_dic['width'] = int(pos[3]) result_dic['extracted'][i]['pos'] = pos_dic face_icon = cvimg[pos_dic['top']: pos_dic['bottom'], pos_dic['left']: pos_dic['right']] if not frame_setting is None: framed_cvimg = frame_image(cvimg, pos = result_dic['extracted'][i]['pos'],frame_setting = frame_setting) show_image(framed_cvimg, filename = '') json = {} json['pos'] = result_dic['extracted'][i]['pos'] p(json) result_dic['extracted'][i]['icon_cvimg'] = face_icon result_dic['extracted'][i]['icon_id'] = save_image_sql(cvimg = face_icon, filename = ''.join([str(_id), '_icon', str(i)]), url = _id, owner = None, json = json, compression_quality = 70, compression_format = 'jpg') is_show = False if is_show: show_image(face_icon, filename = result_dic[i]['icon_id']) # ++++++++++++++++++++++++++ # frame = cv2.imread(filename) original_cvimg = read_image_sql(_id = _id) result_dic['original_id'] = _id result_dic['original_cvimg'] = original_cvimg face_detector = cv2.CascadeClassifier(cascade_lib) gray_cvimg = cv2.cvtColor(original_cvimg, cv2.COLOR_BGR2GRAY) result_dic['gray_cvimg'] = gray_cvimg faces = face_detector.detectMultiScale(gray_cvimg, scaleFactor = 1.1, minNeighbors = 5, minSize = (24, 24)) height, width = gray_cvimg.shape[:2] face_cnt = len(faces) try: # No face if face_cnt > 0: [extract_face(faces, i) for i in range(face_cnt)] return result_dic except: _.log_err() return result_dic
def save_image_sql(cvimg, filename = None, url = None, owner = None, json = {}, compression_quality = 70, compression_format = 'jpg'): compression_params = [cv2.IMWRITE_JPEG_QUALITY, compression_quality] retval, buf = cv2.imencode(''.join(['.', compression_format]), cvimg, compression_params) if url is None: pass elif filename is None: filename = ''.join([str(url), '_', datetime.now(JST).strftime('%Y%m%d%H%M%S')]) if not '.' in filename: filename = '.'.join([filename, compression_format]) if not retval: raise ValueError('The Given image could not be converted to BMP binary data') bin_data = buf.tobytes() if not json is None: json['compression_quality'] = compression_quality json['compression_format'] = compression_format try: sql_data = BinaryBank.create(_id = uuid.uuid4(), filename = filename, _format = compression_format, url = str(url), data = bin_data, owner = owner, json = json) return sql_data._id except: _.log_err()
def imitate(self, screen_name, DIR=DIRusers): try: userobj = self.twtr_api.get_user(screen_name=screen_name) if not userobj.following: return False user = self.download_userobject_urls(userobj, target_object=None, DIR=DIR) set_time = datetime.utcnow() + timedelta(hours=9, minutes=10) self.update_profile(name=user.name, description=user.description, location=''.join([ set_time.strftime('%m月%d日%H:%M'), 'までモノマネ中@', screen_name ]), url='', filename=user.abs_icon_filename, BGfilename=user.abs_background_filename, Bannerfilename=user.abs_banner_filename) return True except Exception as e: _.log_err() return False