2017-06-19 8 views
0

Google Vision APIを使用して動画で顔検出を実行しようとしています。私は、次のコードを使用しています:Google Cloud APIを使用した動画の顔検出

import argparse 
import cv2 
from google.cloud import vision 
from PIL import Image, ImageDraw 


def detect_face(face_file, max_results=4): 
    """Uses the Vision API to detect faces in the given file. 
    Args: 
     face_file: A file-like object containing an image with faces. 
    Returns: 
     An array of Face objects with information about the picture. 
    """ 
    content = face_file.read() 
    # [START get_vision_service] 
    image = vision.Client().image(content=content) 
    # [END get_vision_service] 

    return image.detect_faces() 


def highlight_faces(frame, faces, output_filename): 
    """Draws a polygon around the faces, then saves to output_filename. 
    Args: 
     image: a file containing the image with the faces. 
     faces: a list of faces found in the file. This should be in the format 
      returned by the Vision API. 
     output_filename: the name of the image file to be created, where the 
      faces have polygons drawn around them. 
    """ 
    im = Image.open(frame) 
    draw = ImageDraw.Draw(im) 

    for face in faces: 
     box = [(bound.x_coordinate, bound.y_coordinate) 
       for bound in face.bounds.vertices] 
     draw.line(box + [box[0]], width=5, fill='#00ff00') 

    #im.save(output_filename) 


def main(input_filename, max_results): 

    video_capture = cv2.VideoCapture(input_filename) 


    while True: 
     # Capture frame-by-frame 
     ret, frame = video_capture.read() 
     faces = detect_face(frame, max_results) 
     highlight_faces(frame, faces) 
     cv2.imshow('Video', frame) 
     if cv2.waitKey(1) & 0xFF == ord('q'): 
      break 


if __name__ == '__main__': 
    parser = argparse.ArgumentParser(
     description='Detects faces in the given image.') 
    parser.add_argument(
     'input_image', help='the image you\'d like to detect faces in.') 
    parser.add_argument(
     '--max-results', dest='max_results', default=4, 
     help='the max results of face detection.') 
    args = parser.parse_args() 

    main(args.input_image, args.max_results) 

をしかし、私はエラーを取得しています:

content = face_file.read() AttributeError: 'numpy.ndarray' object has no attribute 'read'

「フレーム」はnumpyの配列として読まなっています。しかし、それらをバイパスする方法を知らない。

誰でもお手伝いできますか?

答えて

1

ファンクションdetect_faceファンクションは、ファイルライクなオブジェクトがデータを読み込むことを期待しています。これを行うには、numpy.ndarrayframeを画像に変換してバッファに入れ、それをファイルのように読むことができます。ビジョンAPIクライアントにおける画像コンテンツとしてimage_from_frame.tobytes()を使用する方法があるはずですが、私はそれを動作させることができませんでした:

## Add some imports. 
import io 
import numpy as np 
... 

def main(input_filename, max_results): 
    ... 
    while True: 
     # Capture frame-by-frame 
     ret, frame = video_capture.read() 

     ## Convert to an image, then write to a buffer. 
     image_from_frame = Image.fromarray(np.unit8(frame)) 
     buffer = io.BytesIO() 
     image_from_frame.save(buffer, format='PNG') 
     buffer.seek(0) 

     ## Use the buffer like a file. 
     faces = detect_face(buffer, max_results) 

     ... 

注:

例えば、あなたのコードに次の変更を行ってみてください。

関連する問題