Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取用于Tensorflow模型的相机图像并将其保存到新目录_Python_Tensorflow - Fatal编程技术网

Python 如何获取用于Tensorflow模型的相机图像并将其保存到新目录

Python 如何获取用于Tensorflow模型的相机图像并将其保存到新目录,python,tensorflow,Python,Tensorflow,我刚刚开始使用Tensorflow进行目标检测。我使用这个代码来检测网络摄像头流中的图像。相机每秒大约4帧,我想保存所有拍摄的图像,以便对图像运行OCR。我正在尝试获取图像并将其保存到“img/”文件夹中。如何保存用于运行模型的所有图像 下面是代码的摘录 # Initialize frame rate calculation frame_rate_calc = 1 freq = cv2.getTickFrequency() # Initialize video

我刚刚开始使用Tensorflow进行目标检测。我使用这个代码来检测网络摄像头流中的图像。相机每秒大约4帧,我想保存所有拍摄的图像,以便对图像运行OCR。我正在尝试获取图像并将其保存到“img/”文件夹中。如何保存用于运行模型的所有图像

下面是代码的摘录

    # Initialize frame rate calculation
     frame_rate_calc = 1
    freq = cv2.getTickFrequency()

    # Initialize video stream
    videostream = VideoStream(resolution=(imW,imH),framerate=30).start()
   time.sleep(1)

    #for frame1 in camera.capture_continuous(rawCapture, format="bgr",use_video_port=True):
   while True:

    # Start timer (for calculating frame rate)
    t1 = cv2.getTickCount()

    # Grab frame from video stream
    frame1 = videostream.read()

   # Acquire frame and resize to expected shape [1xHxWx3]
    frame = frame1.copy()
   frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
   frame_resized = cv2.resize(frame_rgb, (width, height))
   input_data = np.expand_dims(frame_resized, axis=0)

   # Normalize pixel values if using a floating model (i.e. if model is non-quantized)
   if floating_model:
        input_data = (np.float32(input_data) - input_mean) / input_std

    # Perform the actual detection by running the model with the image as input
     interpreter.set_tensor(input_details[0]['index'],input_data)
      interpreter.invoke()

      # Retrieve detection results
     boxes = interpreter.get_tensor(output_details[0]['index'])[0] # Bounding box coordinates of detected objects
     classes = interpreter.get_tensor(output_details[1]['index'])[0] # Class index of detected objects
     scores = interpreter.get_tensor(output_details[2]['index'])[0] # Confidence of detected objects
     #num = interpreter.get_tensor(output_details[3]['index'])[0]  # Total number of 
     detected objects (inaccurate and not needed)

     # Loop over all detections and draw detection box if confidence is above minimum threshold
    for i in range(len(scores)):
      if ((scores[i] > min_conf_threshold) and (scores[i] <= 1.0)):

    # Get bounding box coordinates and draw box
    # Interpreter can return coordinates that are outside of image dimensions, need to force them to be within image using max() and min()
    ymin = int(max(1,(boxes[i][0] * imH)))
    xmin = int(max(1,(boxes[i][1] * imW)))
    ymax = int(min(imH,(boxes[i][2] * imH)))
    xmax = int(min(imW,(boxes[i][3] * imW)))
    
    cv2.rectangle(frame, (xmin,ymin), (xmax,ymax), (10, 255, 0), 2)

    # Draw label
    object_name = labels[int(classes[i])] # Look up object name from "labels" array using class index
    label = '%s: %d%%' % (object_name, int(scores[i]*100)) # Example: 'person: 72%'
    labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2) 
   # Get font size
    label_ymin = max(ymin, labelSize[1] + 10) # Make sure not to draw label too close to top of window
    cv2.rectangle(frame, (xmin, label_ymin-labelSize[1]-10), (xmin+labelSize[0], label_ymin+baseLine-10), (255, 255, 255), cv2.FILLED) # Draw white box to put label text in
    cv2.putText(frame, label, (xmin, label_ymin-7), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2) # Draw label text

  # Draw framerate in corner of frame
 cv2.putText(frame,'FPS: {0:.2f}'.format(frame_rate_calc),(30,50),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,0),2,cv2.LINE_AA)

 # All the results have been drawn on the frame, so it's time to display it.
 cv2.imshow('Object detector', frame)
#初始化帧速率计算
帧速率计算=1
freq=cv2.getTickFrequency()
#初始化视频流
视频流=视频流(分辨率=(imW,imH),帧速率=30)
时间。睡眠(1)
#对于camera.capture\u continuous中的帧1(rawCapture,format=“bgr”,使用\u video\u port=True):
尽管如此:
#启动计时器(用于计算帧速率)
t1=cv2.getTickCount()
#从视频流抓取帧
frame1=videostream.read()
#获取帧并调整大小至预期形状[1xHxWx3]
frame=frame1.copy()
帧_rgb=cv2.CVT颜色(帧,cv2.COLOR_BGR2RGB)
帧大小=cv2。调整大小(帧大小(宽度、高度))
输入数据=np。展开dims(调整帧大小,轴=0)
#如果使用浮动模型(即,如果模型是非量化的),则标准化像素值
如果是浮动U型:
输入数据=(np.32(输入数据)-输入平均值)/输入标准
#通过以图像作为输入运行模型来执行实际检测
解释器。设置解释器张量(输入解释器详细信息[0]['index'],输入解释器数据)
invoke()解释器
#检索检测结果
box=解释器。获取_张量(输出_详细信息[0]['index'])[0]#检测对象的边界框坐标
classes=解释器。获取_张量(输出_详细信息[1]['index'])[0]#检测到的对象的类索引
分数=解释器。获取张量(输出详细信息[2]['index'])[0]#检测对象的置信度
#num=解释器。获取\u张量(输出\u详细信息[3]['index'])[0]#解释器总数
检测到的对象(不准确且不需要)
#如果置信度高于最小阈值,则循环所有检测并绘制检测框
对于范围内的i(len(分数)):
如果((分数[i]>最小值)和(分数[i]