python - 运行python代码后如何修复CMD卡住
问题描述
所以...正如标题所说,我有一个 python 脚本,它基本上是 yolo 并从给定的图像中找到车牌。它做对了!完美!但问题是,最近当我像往常一样从 cmd 调用它时,通过键入“python yolo.py”它会运行并完成我想要的工作,但它卡住了!而且我根本无法输入任何其他命令。我必须关闭 cmd 并再次运行它。问题是这个脚本假设用另一个脚本调用,它是一个 GUI,当 yolo 运行时我不能再使用这个程序,应该关闭它。我发现这个脚本,没有任何改变,将在其他计算机上完美运行。有人知道我该如何解决吗?
import numpy as np
import time
import cv2
INPUT_FILE= ('Outputs/original.jpg')
OUTPUT_FILE=('Outputs/_yolo.jpg')
LABELS_FILE='data/classes.names'
CONFIG_FILE='data/yolov3.cfg'
WEIGHTS_FILE='data/lapi.weights'
CONFIDENCE_THRESHOLD=0.3
LABELS = open(LABELS_FILE).read().strip().split("\n")
np.random.seed(4)
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3),
dtype="uint8")
net = cv2.dnn.readNetFromDarknet(CONFIG_FILE, WEIGHTS_FILE)
image = cv2.imread(INPUT_FILE)
(H, W) = image.shape[:2]
# determine only the *output* layer names that we need from YOLO
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416),
swapRB=True, crop=False)
net.setInput(blob)
start = time.time()
layerOutputs = net.forward(ln)
end = time.time()
print("[INFO] YOLO took {:.6f} seconds".format(end - start))
# initialize our lists of detected bounding boxes, confidences, and
# class IDs, respectively
boxes = []
confidences = []
classIDs = []
# loop over each of the layer outputs
for output in layerOutputs:
# loop over each of the detections
for detection in output:
# extract the class ID and confidence (i.e., probability) of
# the current object detection
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
# filter out weak predictions by ensuring the detected
# probability is greater than the minimum probability
if confidence > CONFIDENCE_THRESHOLD:
# scale the bounding box coordinates back relative to the
# size of the image, keeping in mind that YOLO actually
# returns the center (x, y)-coordinates of the bounding
# box followed by the boxes' width and height
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
# use the center (x, y)-coordinates to derive the top and
# and left corner of the bounding box
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
# update our list of bounding box coordinates, confidences,
# and class IDs
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
# apply non-maxima suppression to suppress weak, overlapping bounding
# boxes
idxs = cv2.dnn.NMSBoxes(boxes, confidences, CONFIDENCE_THRESHOLD,
CONFIDENCE_THRESHOLD)
# ensure at least one detection exists
if len(idxs) > 0:
# loop over the indexes we are keeping
for i in idxs.flatten():
# extract the bounding box coordinates
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
color = [int(c) for c in COLORS[classIDs[i]]]
cv2.rectangle(image, (x, y), (x + w, y + h), color, 20)
text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i])
cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
0.5, color, 20)
# save the output image
ratio = 490.0 / image.shape[1]
dim = (490, int(image.shape[0] * ratio))
resized_img = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
cv2.imwrite(('Outputs/_yolo.jpg'), resized_img)
cropped_img = cv2.imread('Outputs/original.jpg')
cropped_img = cropped_img[y:y+h, x:x+w]
ratio = 490.0 / cropped_img.shape[1]
dim = (490, int(cropped_img.shape[0] * ratio))
resized_img = cv2.resize(cropped_img, dim, interpolation = cv2.INTER_AREA)
cv2.imwrite(('Outputs/license.jpg'), resized_img)
脚本结束后 CMD 看起来像这样
更新:我在原始脚本的末尾添加了 threading.enumerate() 并且输出是这样的
解决方案
推荐阅读
- java - CDI 在 jberet 实现下无法与 Java EE Batch 一起使用
- swift - 由于 MKPointAnnotation 上无法识别的选择器而导致崩溃
- angular - 如何防止在Angular中选择下拉值
- java - Apache Mina:如何编写 Http 服务器?
- php - 如何使用 .HTACCESS 文件将网站 URL 重定向到 HTTPS://
- ios - 在 iOS 中更新 UI 元素的单元测试方法
- r - $ 中的错误:“闭包”类型的对象不是子集,我该如何解决?
- java - 如何在Java中将包含字符串列表的JsonNode转换为逗号分隔的字符串
- python - 使用 urllib 时出现 400 Bad Request HTTPError 异常
- reactjs - 访问从 redux thunk 函数获取的数据的正确方法是什么?