Python 从裁剪后的图像中获取原始颜色yolov3
这是我第一次尝试YOLov3。我试图在YOLOv3预测的边界框内获得裁剪图像。我能够获得裁剪后的图像。问题是,图像的颜色与原始裁剪图像的颜色不同。我想把这个裁剪过的图像传递到另一个模型中,这样我就可以确认裁剪过的图像中确实有火 当裁剪的图像与原始图像不同时,我不能这样做。任何帮助都将不胜感激 以下是裁剪图像的代码:Python 从裁剪后的图像中获取原始颜色yolov3,python,deep-learning,computer-vision,yolo,Python,Deep Learning,Computer Vision,Yolo,这是我第一次尝试YOLov3。我试图在YOLOv3预测的边界框内获得裁剪图像。我能够获得裁剪后的图像。问题是,图像的颜色与原始裁剪图像的颜色不同。我想把这个裁剪过的图像传递到另一个模型中,这样我就可以确认裁剪过的图像中确实有火 当裁剪的图像与原始图像不同时,我不能这样做。任何帮助都将不胜感激 以下是裁剪图像的代码: import cv2 import numpy as np import glob import random from scipy import ndimage import ma
import cv2
import numpy as np
import glob
import random
from scipy import ndimage
import matplotlib.pyplot as plt
from PIL import Image
# Load Yolo
net = cv2.dnn.readNet("D:\obj_detection\yolo_custom_detection\yolov3_training_last.weights", "D:\obj_detection\yolo_custom_detection\yolov3_testing.cfg")
# Name custom object
classes = ["fire"]
# Images path
images_path = glob.glob(r"D:\obj_detection\test_img\*.jpg")
layer_names = net.getLayerNames() #Get the name of all layers of the network.
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] #Get the index of the output layers.
#These two functions are used for getting the output layers (82,94,106).
colors = np.random.uniform(0, 255, size=(len(classes), 3))
# Insert here the path of your images
random.shuffle(images_path)
# loop through all the images
for img_path in images_path:
# Loading image
img = cv2.imread(img_path)
img = cv2.resize(img, None, fx=0.4, fy=0.4)
height, width, channels = img.shape
# Detecting objects
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
# Syntax: blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size, mean, swapRB=True)
net.setInput(blob)
outs = net.forward(output_layers)
# Showing informations on the screen
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.3:
# Object detected
print(class_id)
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) #Non max suppression
print(indexes)
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
if x<0:
x = 0
if y<0:
y = 0
if w<0:
w = 0
if h<0:
h = 0
print("The box dimensions:",boxes[i])
print(img_path)
label = str(classes[class_ids[i]])
color = colors[class_ids[i]]
print("Dimensions of x:", x,",y:",y,",x+w:", x+w, ",y+h:", y+h)
cropped_image = img[y:y+h, x:x+w]
plt.imshow(cropped_image)
cv2.rectangle(img, (x, y), (x + w, y + h), color, 1)
cv2.putText(img, label, (x, y + 30), font, 0.8, color, 2)
img = cv2.resize(img, (700, 600))
cv2.imshow("Fire Detection", img)
cv2.resizeWindow("Fire Detection", 1600,1200)
key = cv2.waitKey(0)
if key == ord('q'):
break
cv2.destroyAllWindows()
导入cv2
将numpy作为np导入
导入glob
随机输入
从scipy导入ndimage
将matplotlib.pyplot作为plt导入
从PIL导入图像
#加载Yolo
net=cv2.dnn.readNet(“D:\obj\u detection\yolo\u custom\u detection\yolov3\u training\u last.weights”,“D:\obj\u detection\yolo\u custom\u detection\yolov3\u testing.cfg”)
#命名自定义对象
类别=[“火灾”]
#图像路径
images\u path=glob.glob(r“D:\obj\u detection\test\u img\*.jpg”)
layer_names=net.getLayerNames()#获取网络所有层的名称。
output_layers=[layer_names[i[0]-1]表示网络中的i.getUnconnectedOutLayers()]#获取输出层的索引。
#这两个函数用于获取输出层(82,94106)。
颜色=np.random.uniform(0,255,大小=(等级,3))
#在此插入图像的路径
随机移动(图像路径)
#循环浏览所有图像
对于图像中的img\u路径:
#加载图像
img=cv2.imread(img\u路径)
img=cv2.调整大小(img,无,fx=0.4,fy=0.4)
高度、宽度、通道=img.shape
#探测对象
blob=cv2.dnn.blobFromImage(img,0.00392,(416416),(0,0,0),True,crop=False)
#语法:blob=cv2.dnn.blobFromImage(image,scalefactor=1.0,size,mean,swapRB=True)
net.setInput(blob)
outs=净正向(输出层)
#在屏幕上显示信息
类_id=[]
信任=[]
框=[]
对于out-in-out:
对于检测输入输出:
分数=检测[5:]
class_id=np.argmax(分数)
信心=分数[班级id]
如果置信度>0.3:
#检测到的对象
打印(class_id)
中心x=int(检测[0]*宽度)
中心y=int(检测[1]*高度)
w=int(检测[2]*宽度)
h=int(检测[3]*高度)
#直角坐标
x=int(中心x-w/2)
y=int(中心y-h/2)
框。追加([x,y,w,h])
信任。附加(浮动(信任))
类id.append(类id)
索引=cv2.dnn.NMSBoxes(盒子,机密,0.5,0.4)#非最大值抑制
打印(索引)
font=cv2.font\u HERSHEY\u COMPLEX\u SMALL
对于范围内的i(len(框)):
如果我在索引中:
x、 y,w,h=盒子[i]
如果席了解这是怎么发生的。显然,在OpenCV中,图像实际上是按BGR顺序表示的,而不是RGB!plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))添加了这一行,它就像一个魅力。希望这对任何寻求帮助的人都有帮助。我明白这是怎么发生的。显然,在OpenCV中,图像实际上是按BGR顺序表示的,而不是RGB!plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))添加了这一行,它就像一个魅力。希望这能帮助任何寻求帮助的人。