例えば、物体検出などのタスクではどこにどんな物体があると検出したかを可視化するために、赤枠を画像に重ねることが多い。 画像は予測のインプットにするためにndarrayにされていることが多いので、以下のようにndarrayをImageオブジェクトに変換し、それに 多角形を描画する。
shape = dLicense.edited_image.shape image: np.ndarray = dLicense.edited_image.reshape(1, shape[0], shape[1], shape[2]) pred: np.ndarray = model.predict(image)[0] pil_img: Image = Image.fromarray(np.uint8(cv2.cvtColor(dLicense.edited_image, cv2.COLOR_BGR2RGB))) draw_img: ImageDraw = ImageDraw.Draw(pil_img) draw_img.polygon(((pred[0], pred[1]), (pred[2], pred[3]), (pred[4], pred[5]), (pred[6], pred[7])), outline=(255, 0, 0)) pil_img.save("tmp.jpg")
参考: