Exemplo n.º 1
0
def evaluation(img_path):
  ckpt_path = CKPT_PATH
  # GraphのReset(らしいが、何をしているのかよくわかっていない…)
  tf.reset_default_graph()
  # データを入れる配列
  image = []
  img = cv2.imread(img_path)
  img = cv2.resize(img, (28, 28))

  # 画像情報を一列にした後、0-1のfloat値にする
  image.append(img.flatten().astype(np.float32)/255.0)
  # numpy形式に変換し、TensorFlowで処理できるようにする
  image = np.asarray(image)
  # 入力画像に対して、各ラベルの確率を出力して返す(main.pyより呼び出し)
  logits = main.inference(image, 1.0)
  # We can just use 'c.eval()' without passing 'sess'
  sess = tf.InteractiveSession()
  # restore(パラメーター読み込み)の準備
  saver = tf.train.Saver()
  # 変数の初期化
  sess.run(tf.initialize_all_variables())
  if ckpt_path:
    # 学習後のパラメーターの読み込み
    saver.restore(sess, ckpt_path)
  # sess.run(logits)と同じ
  softmax = logits.eval()
  # 判定結果
  result = softmax[0]

  # 判定結果を%にして四捨五入
  rates = [round(n * 100.0, 1) for n in result]
  humans = []
  # ラベル番号、名前、パーセンテージのHashを作成
  for index, rate in enumerate(rates):
    name = HUMAN_NAMES[index]
    humans.append({
      'label': index,
      'name': name,
      'rate': rate
    })
  # パーセンテージの高い順にソート
  rank = sorted(humans, key=lambda x: x['rate'], reverse=True)

  # 判定結果を返す
  return rank
Exemplo n.º 2
0
def evaluation(img_path, ckpt_path):
  # GraphのReset(らしいが、何をしているのかよくわかっていない…)
  tf.reset_default_graph()
  # ファイルを開く
  f = open(img_path, 'r')
  img = cv2.imread(img_path, cv2.IMREAD_COLOR)
  # モノクロ画像に変換
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  face = faceCascade.detectMultiScale(gray, 1.1, 3)
  if len(face) > 0:
    for rect in face:
      # 加工した画像に何でもいいので適当な名前をつけたかった。日付秒数とかでいいかも
      random_str = str(random.random())
      # 顔部分を赤線で書こう
      cv2.rectangle(img, tuple(rect[0:2]), tuple(rect[0:2]+rect[2:4]), (0, 0, 255), thickness=2)
      # 顔部分を赤線で囲った画像の保存先
      face_detect_img_path = './static/images/face_detect/' + random_str + '.jpg'
      # 顔部分を赤線で囲った画像の保存
      cv2.imwrite(face_detect_img_path, img)
      x = rect[0]
      y = rect[1]
      w = rect[2]
      h = rect[3]
      # 検出した顔を切り抜いた画像を保存
      cv2.imwrite('./static/images/cut_face/' + random_str + '.jpg', img[y:y+h, x:x+w])
      # TensorFlowへ渡す切り抜いた顔画像
      target_image_path = './static/images/cut_face/' + random_str + '.jpg'
  else:
    # 顔が見つからなければ処理終了
    print 'image:NoFace'
    return
  f.close()

  f = open(target_image_path, 'r')
  # データを入れる配列
  image = []
  img = cv2.imread(target_image_path)
  img = cv2.resize(img, (28, 28))
  # 画像情報を一列にした後、0-1のfloat値にする
  image.append(img.flatten().astype(np.float32)/255.0)
  # numpy形式に変換し、TensorFlowで処理できるようにする
  image = np.asarray(image)
  # 入力画像に対して、各ラベルの確率を出力して返す(main.pyより呼び出し)
  logits = main.inference(image, 1.0)
  # We can just use 'c.eval()' without passing 'sess'
  sess = tf.InteractiveSession()
  # restore(パラメーター読み込み)の準備
  saver = tf.train.Saver()
  # 変数の初期化
  sess.run(tf.initialize_all_variables())
  if ckpt_path:
    # 学習後のパラメーターの読み込み
    saver.restore(sess, ckpt_path)
  # sess.run(logits)と同じ
  softmax = logits.eval()
  # 判定結果
  result = softmax[0]
  # 判定結果を%にして四捨五入
  rates = [round(n * 100.0, 1) for n in result]
  humans = []
  # ラベル番号、名前、パーセンテージのHashを作成
  for index, rate in enumerate(rates):
    name = HUMAN_NAMES[index]
    humans.append({
      'label': index,
      'name': name,
      'rate': rate
    })
  # パーセンテージの高い順にソート
  rank = sorted(humans, key=lambda x: x['rate'], reverse=True)

  # 判定結果と加工した画像のpathを返す
  return [rank, face_detect_img_path, target_image_path]
Exemplo n.º 3
0
    flag = True
    print("NOW RUNNNING")
    while True:
        cursor = userchat.find({"type":"sent", "replied":False})
        for document in cursor:
            mid = document['mid']
            message = document['message'].lower()
            sender = document['sender']
            if flag:
                reply = str(bot.get_response(message))
                if message=='second layer':
                    flag = False
                    reply = "Second Layer Activated"
            else:
                if message == 'first layer':
                    flag = True
                    reply = "First Layer Activated"
                    continue
                top_n = 3
                sentence = m.inference(seed, top_n)
                reply = ''.join(sentence)
            now = datetime.now() # current date and time
            date = now.strftime("%d-%m-%Y")
            time = now.strftime("%H:%M:%S")
            print(document,)
            print("REPLY")
            print(reply)
            print("\n\n\n")
            userchat.insert({"type":"recieved", "mid":mid, "sender":sender, "message":reply, "time":time, "date":date})
            userchat.update_one({"mid":mid, "sender":sender},{"$set":{"replied":True}})
    # Load trained weights (and extra objects needed) for our model
    c_path = rf'c_object_trained_on_train1test1.wtag.pkl'
    f_path = rf'f_object_trained_on_train1test1.wtag.pkl'
    weights_path = rf'weights_trained_on_train1test1.wtag.pkl'
    with open(c_path, 'rb') as file:
        c = pickle.load(file)
    with open(f_path, 'rb') as file:
        f = pickle.load(file)
    with open(weights_path, 'rb') as file:
        weights = pickle.load(file)

    # Run inference on competition 1 data file and write results to file according to .wtag format (described in HW1)
    main.inference(path_file_to_tag=r'comp1.words',
                   path_result=r'comp_m1_308044296.wtag',
                   weights=weights,
                   features_indices=f,
                   class_statistics=c,
                   beam=50)
    """ -------------------------------- """
    """ PREPARE COMP 2 FILE FROM WEIGHTS """

    # Load trained weights (and extra objects needed) for our model
    c_path = rf'c_object_trained_on_train2.wtag.pkl'
    f_path = rf'f_object_trained_on_train2.wtag.pkl'
    weights_path = rf'weights_trained_on_train2.wtag.pkl'
    with open(c_path, 'rb') as file:
        c = pickle.load(file)
    with open(f_path, 'rb') as file:
        f = pickle.load(file)
    with open(weights_path, 'rb') as file:
        weights = pickle.load(file)