我有一个涉及flask和python的程序,它实际上是一个更大的程序的一部分,它可以识别车牌并同时做其他事情,我想访问一个while循环内的变量,该循环在函数内。代码底部的boxes变量就是我所说的变量,但无论我怎么尝试,都是不可能的。最后的结果是:NameError: name 'boxes' is not defined.我现在很困惑如何解决这个问题,有谁能帮我?在这些情况下,如何从函数外部访问变量?即使我将变量更改为global,它仍然无法访问。这是我尝试的方法之一
这是Flask.py
from flask import Flask, render_template, Response
from camera import CameraStream
import cv2
import os
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils
from object_detection.builders import model_builder
from object_detection.utils import config_util
import tensorflow as tf
from keras.models import load_model
from keras.preprocessing.image import img_to_array
import numpy as np
import functools
import imutils
import math
from datetime import datetime
import mysql.connector as mysql
import time
app = Flask(__name__)
cap = CameraStream().start()
@app.route('/')
def index():
"""Video streaming home page."""
return render_template('index.html')
##################################################################################
#### Load Check points, config files, labelmap and Detect License plate Object ###
##################################################################################
CONFIG_PATH = './pipeline.config'
CHECKPOINT_PATH = './training'
# Load pipeline config and build a detection model
configs = config_util.get_configs_from_pipeline_file(CONFIG_PATH)
detection_model = model_builder.build(model_config=configs['model'], is_training=False)
# Restore checkpoint
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt.restore(os.path.join(CHECKPOINT_PATH, 'ckpt-52')).expect_partial()
@tf.function
def detect_fn(image):
image, shapes = detection_model.preprocess(image)
prediction_dict = detection_model.predict(image, shapes)
detections = detection_model.postprocess(prediction_dict, shapes)
return detections
category_index = label_map_util.create_category_index_from_labelmap('label_map.pbtxt')
def streaming():
while cap:
frame = cap.read()
# Check That If Frame Is Capturing Or Not
if frame is None:
print("disconnected!")
# You Can Adjust The Streaming Window size
frame = cv2.resize(frame, (0,0), fx=0.4, fy=0.4)
image_np = np.array(frame)
input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
detections = detect_fn(input_tensor)
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
for key, value in detections.items()}
detections['num_detections'] = num_detections
# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
label_id_offset = 1
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes']+label_id_offset,
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=5,
min_score_thresh=.8,
agnostic_mode=False)
detection_thereshold = 0.7
image = image_np_with_detections
# Scores, boxes and classes above threhold
scores = list(filter(lambda x: x> detection_thereshold, detections['detection_scores']))
global boxes
boxes = detections['detection_boxes'][:len(scores)]
classes = detections['detection_classes'][:len(scores)]
frame = cv2.imencode('.jpg', image_np_with_detections)[1].tobytes()
yield (b'--framern'
b'Content-Type: image/jpegrnrn' + frame + b'rn') # concate frame one by one and show result
print(boxes)
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(streaming(),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='127.0.0.1', threaded=True)
这是camera.py
from threading import Thread, Lock
import cv2
class CameraStream(object):
def __init__(self, src="rtsp://admin:admin@192.168.1.2:554/1/1"):
self.stream = cv2.VideoCapture(src)
(self.grabbed, self.frame) = self.stream.read()
self.started = False
self.read_lock = Lock()
def start(self):
if self.started:
print("already started!!")
return None
self.started = True
self.thread = Thread(target=self.update, args=())
self.thread.start()
return self
def update(self):
while self.started:
(grabbed, frame) = self.stream.read()
self.read_lock.acquire()
self.grabbed, self.frame = grabbed, frame
self.read_lock.release()
def read(self):
self.read_lock.acquire()
frame = self.frame.copy()
self.read_lock.release()
return frame
def stop(self):
self.started = False
self.thread.join()
def __exit__(self, exc_type, exc_value, traceback):
self.stream.release()
除了在函数内部,我找不到任何地方声明的boxes
变量。
如果您想要使用global
关键字,则需要有一个具有该名称的全局变量。长话短说,全局变量需要在任何函数之外声明。
你可能想这样做:
boxes = None # or whatever you want the initial value to be
def streaming():
....
global boxes
boxes = detections['detection_boxes'][:len(scores)]
....
print(boxes)
但是,请记住,使用全局变量不被认为是一个好的实践。如果在streaming()
方法中覆盖BOXES
变量,为什么需要它是全局的?如果只是为了打印,可以在函数内部打印。
你可以在这里阅读更多关于全局变量的内容:为什么全局变量是邪恶的?