眉上风止825 2023-06-23 09:00 采纳率: 0%
浏览 7

关于#神经网络#的问题:当采集了一些照片后想要停止人脸采集应该怎样做

如下述代码,当采集了一些照片后想要停止人脸采集应该怎样做

import cv2
import dlib
import numpy as np
detector=dlib.get_frontal_face_detector()
cap=cv2.VideoCapture(0)
frame_count=0
face_count=0
font=cv2.FONT_HERSHEY_SIMPLEX
import keyboard
import sys
while True:
    if keyboard.is_pressed('q'):
        print('User pressed the "q" key, stopping image capture...')
        sys.exit()
while True:
    ret,frame=cap.read()
    if (ret !=True):
        print('没有捕获摄像头,数据采集结束或者检查摄像头是否正常工作!')
        break
    frame_count+=1
    detected=detector(frame,1)
    faces=[]
    if len(detected)>0:
        for i,d in enumerate(detected):
            face_count+=1
            x1,y1,x2,y2,w,h=d.left(),d.top(),d.right()+1,d.bottom()+1,d.width(),d.height()
            face=frame[y1:y2+1,x1:x2+1,:]
            if (frame_count % 4 !=0):
                file_name="./dataset/train/one/"+str(frame_count)+"_one"+str(i)+".jpg"
            else:
                file_name="./dataset/valid/one/"+str(frame_count)+"_one"+str(i)+".jpg"
            cv2.imwrite(file_name,face)
            cv2.rectangle(frame,(x1,y1),(x2,y2),(0,255,0),2)
            cv2.putText(frame,f"already get:{frame_count},faces",\
                        (80,80),font,1.2,(255,0,0),3)
    cv2.imshow("Face Detector",frame)
    if cv2.waitKey(1) & 0xFF ==27:
        break
print('已经完成了{0}帧检测,共保存了{1}幅脸部图像'.format(frame_count,face_count))
cap.release()
cv2.destoryAllWindows()
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-06-23 10:01
    关注
    • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7653162
    • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:深度学习中对图片和标签执行相同位置裁剪及其他变换操作
    • 除此之外, 这篇博客: 自编码器的实现与应用中的 自编码器的数据相似性,我们用来测试的数据仍然是 手写数字 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    • x_train.shape, x_test.shape
      
      x_train = np.expand_dims(x_train, -1)
      x_test = np.expand_dims(x_test, -1)
      
      x_train.shape, x_test.shape
      

      在这里插入图片描述

      x_train = tf.cast(x_train, tf.float32)/255
      x_test = tf.cast(x_test, tf.float32)/255
      
      noise_factor = 0.5
      x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape) 
      x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape) 
      
      x_train_noisy = np.clip(x_train_noisy, 0., 1.)
      x_test_noisy = np.clip(x_test_noisy, 0., 1.)
      
      n = 10
      
      plt.figure(figsize=(10, 2))
      for i in range(1, n):    
      # 展示原始图像
          ax = plt.subplot(1, n, i)
          plt.imshow(x_train_noisy[i].reshape(28, 28))
      plt.show()
      

      在这里插入图片描述

      input = tf.keras.layers.Input(shape=(28, 28, 1))
      
      # Encoder
      x = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input)
      x = tf.keras.layers.MaxPool2D((2, 2), padding='same')(x)   # 14*14*16
      
      x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
      x = tf.keras.layers.MaxPool2D((2, 2), padding='same')(x)   # 7*7*32
      
      
      # Decoder
      x = tf.keras.layers.Conv2DTranspose(16, (3, 3), strides=2,
                                 activation='relu', padding='same')(x)   # 14*14*16
      output = tf.keras.layers.Conv2DTranspose(1, (3, 3), strides=2,
                                 activation='sigmoid', padding='same')(x)   # 28*28*1
      
      model = tf.keras.Model(inputs=input, outputs=output)
      
      model.compile(optimizer='adam', loss='mse')
      
      model.fit(x_train_noisy, x_train,
                      nb_epoch=50,
                      batch_size=256,
                      validation_data=(x_test_noisy, x_test))
      

      在这里插入图片描述

      pre_test = model.predict(x_test_noisy)
      
      plt.figure(figsize=(20, 4))
      for i in range(1, n):    
      # 展示原始图像
          ax = plt.subplot(2, n, i)
          plt.imshow(x_test_noisy[i].reshape(28, 28))  
          # 展示自编码器重构后的图像
          ax = plt.subplot(2, n, i + n)
          plt.imshow(pre_test[i].reshape(28, 28))
      plt.show()
      

      在这里插入图片描述

      model.layers
      

      在这里插入图片描述


    • 您还可以看一下 田德华老师的小程序·云开发实战 微信朋友圈所有功能课程中的 实现个人主页图片预览功能小节, 巩固相关知识点
    评论

报告相同问题?

问题事件

  • 创建了问题 6月23日

悬赏问题

  • ¥15 treelib库有读取树数据方法吗?
  • ¥15 咨询一个PYTHON的问题
  • ¥15 机器学习建模调参,roc评价指标
  • ¥15 RCS plot 包内置数据集使用时报错,如何解决?
  • ¥15 keil+mspm0g3507+二维总线舵机
  • ¥15 如何用wireshark分析找出url接口和param参数
  • ¥15 有谁知道这是阿里云那个应用的域名吗,怎么调用?
  • ¥30 正则表达式的一些问题
  • ¥15 C#如何使用不需要安装 Microsoft Excel 的机器上的方法或者库实现:将指定Excel区域导出为图片(例如A1:AO50)
  • ¥15 虚拟机只能接收不能发送