Python 如何从经过培训的.pb模型中获取标签并将其打印在colab中的图像上?
我正在colab中开发一个简单的2D活性检测项目。我在计算机视觉方面没有经验,所以在视觉方面我完全没有经验。我将转移学习用于一个初始网络(在imagenet上培训),并将其应用于我自己的问题。但我不确定我的方法是否正确,所以我想用电脑摄像头来测试一下。我保存的模型目录结构如下所示:Python 如何从经过培训的.pb模型中获取标签并将其打印在colab中的图像上?,python,visualization,google-colaboratory,Python,Visualization,Google Colaboratory,我正在colab中开发一个简单的2D活性检测项目。我在计算机视觉方面没有经验,所以在视觉方面我完全没有经验。我将转移学习用于一个初始网络(在imagenet上培训),并将其应用于我自己的问题。但我不确定我的方法是否正确,所以我想用电脑摄像头来测试一下。我保存的模型目录结构如下所示: | inception |-assets |-variables |-variables.data-00000-of-00001 |-variables.index |-saved_model.pb 我还有
|
inception
|-assets
|-variables
|-variables.data-00000-of-00001
|-variables.index
|-saved_model.pb
我还有其他面部检测的必需品,我使用colab与摄像头取得联系:
import imutils
import numpy as np
import cv2
from google.colab.patches import cv2_imshow
from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode
def take_photo(filename='photo.jpg', quality=0.8):
js = Javascript('''
async function takePhoto(quality) {
const div = document.createElement('div');
const capture = document.createElement('button');
capture.textContent = 'Capture';
div.appendChild(capture);
const video = document.createElement('video');
video.style.display = 'block';
const stream = await navigator.mediaDevices.getUserMedia({video: true});
document.body.appendChild(div);
div.appendChild(video);
video.srcObject = stream;
await video.play();
// Resize the output to fit the video element.
google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
// Wait for Capture to be clicked.
await new Promise((resolve) => capture.onclick = resolve);
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
stream.getVideoTracks()[0].stop();
div.remove();
return canvas.toDataURL('image/jpeg', quality);
}
''')
display(js)
data = eval_js('takePhoto({})'.format(quality))
binary = b64decode(data.split(',')[1])
with open(filename, 'wb') as f:
f.write(binary)
return filename
使用本地相机拍照并显示的功能
image_file = take_photo()
#image = cv2.imread(image_file, cv2.IMREAD_UNCHANGED)
image = cv2.imread(image_file)
# resize it to have a maximum width of 400 pixels
image = imutils.resize(image, width=400)
(h, w) = image.shape[:2]
print(w,h)
cv2_imshow(image)
获取用于人脸检测的caffemodel和deploy.prototxt
!wget -N https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt
!wget -N https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodel
for i in range(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with the prediction
confidence = detections[0, 0, i, 2]
# filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence threshold
if confidence > 0.5:
# compute the (x, y)-coordinates of the bounding box for the object
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# draw the bounding box of the face along with the associated probability
text = "{:.2f}%".format(confidence * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
cv2.putText(image, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
加载模型(不是初始模型,因为这就是这个问题的主题)
调整图像大小
image = imutils.resize(image, width=400)
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
print("[INFO] computing object detections...")
net.setInput(blob)
detections = net.forward()
仅使用人脸检测的标签
!wget -N https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt
!wget -N https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodel
for i in range(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with the prediction
confidence = detections[0, 0, i, 2]
# filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence threshold
if confidence > 0.5:
# compute the (x, y)-coordinates of the bounding box for the object
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# draw the bounding box of the face along with the associated probability
text = "{:.2f}%".format(confidence * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
cv2.putText(image, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
显示图像
cv2_imshow(image)
问题是如何调整此代码块以包含保存的_model.pb model。模型有两个标签,假的和真的。我需要得到这些标签,并打印在图像上。目前,这些代码块只在检测到的人脸和打印区域外绘制正方形。说到可视化,我不知道从哪里开始,也不知道如何访问.pb模型及其标签
我非常感谢你的帮助
这是我的第一个问题,我不确定格式是否正确