import cv2 import numpy as np import torch as torch from torchvision.models import densenet121 # Load the DenseNet model model = densenet121(pretrained=True) # Read the image image = cv2.imread('C:/Users/23594/Desktop/888.jpg') # Convert the image to grayscale grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Resize the image to the size of the model input resized_image = cv2.resize(grayscale_image, (224, 224)) # Normalize the image normalized_image = resized_image / 255.0 # Convert the image to a tensor image_tensor = torch.from_numpy(normalized_image).float() # Predict the key points of the person predictions = model(image_tensor) # Convert the predictions to a list of points points = [] for i in range(len(predictions[0])): points.append((predictions[0][i][0], predictions[0][i][1])) # Draw the key points on the image cv2.drawKeypoints(image, points, np.array([]), (0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # Display the image cv2.imshow('Image', image) cv2.waitKey(0)import cv2 import numpy as np import torch as torch from torchvision.models import densenet121 # Load the DenseNet model model = densenet121(pretrained=True) # Read the image image = cv2.imread('C:/Users/23594/Desktop/888.jpg') # Convert the image to grayscale grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Resize the image to the size of the model input resized_image = cv2.resize(grayscale_image, (224, 224)) # Normalize the image normalized_image = resized_image / 255.0 # Convert the image to a tensor image_tensor = torch.from_numpy(normalized_image).float() # Predict the key points of the person predictions = model(image_tensor) # Convert the predictions to a list of points points = [] for i in range(len(predictions[0])): points.append((predictions[0][i][0], predictions[0][i][1])) # Draw the key points on the image cv2.drawKeypoints(image, points, np.array([]), (0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # Display the image cv2.imshow('Image', image) cv2.waitKey(0)
时间: 2024-04-02 15:33:30 浏览: 130
这段代码是使用预训练的DenseNet模型对一张人物图片进行关键点检测,并在图片上标出关键点。它使用了OpenCV和PyTorch库。具体流程包括:
1. 导入必要的库,包括OpenCV、NumPy和PyTorch等。
2. 加载预训练的DenseNet模型。
3. 读取待检测的人物图片,并将其转为灰度图像。
4. 调整图片大小为与模型输入大小相同。
5. 将像素值归一化到0到1之间,并将其转为PyTorch的张量。
6. 对张量进行预测,得到关键点的坐标。
7. 将预测结果转为关键点的列表。
8. 在原始图片上标出关键点。
9. 显示标注后的图片。
需要注意的是,这段代码没有进行任何异常处理和参数调优,仅仅是演示了一个基本的关键点检测流程。
相关问题
import torchimport cv2import numpy as npfrom models.experimental import attempt_loadfrom utils.general import non_max_suppressionclass YoloV5Detector: def __init__(self, model_path, conf_thresh=0.25, iou_thresh=0.45): self.conf_thresh = conf_thresh self.iou_thresh = iou_thresh self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.model = attempt_load(model_path, map_location=self.device) self.model.eval() def detect(self, image_path): img = cv2.imread(image_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = img.transpose(2, 0, 1) img = np.ascontiguousarray(img) img = torch.from_numpy(img).to(self.device).float() / 255.0 # Run inference with torch.no_grad(): results = self.model(img, size=img.shape[-2:]) results = non_max_suppression(results, conf_thres=self.conf_thresh, iou_thres=self.iou_thresh) return results
这是一个使用 YOLOv5 模型进行目标检测的 Python 代码。该类 YoloV5Detector 包含了模型加载、图片预处理和推理的功能。具体来说,会使用 OpenCV 库读取图片,将其转换为 RGB 格式,然后转换为 PyTorch 的 Tensor 格式,并将其送入 YOLOv5 模型中进行推理。最后,使用非极大值抑制算法(NMS)筛选出检测出来的物体,并返回结果。其中,conf_thresh 和 iou_thresh 分别表示置信度和重叠阈值,可以通过调整这两个参数来控制检测结果的准确率和召回率。
import torch from djitellopy import Tello import cv2 import numpy as np import models from models import yolo def get_model(): # 假设 'yolov5s.yaml' 是 yolov5s 模型的定义文件的路径 model = models.yolo.Model('models/yolov5s.yaml') checkpoint = torch.load('weights/yolov5s.pt') model.load_state_dict(checkpoint['model']) model.eval() return model def preprocess_frame(img): img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (640, 640)) # 将图像大小调整为模型的输入大小 img = img / 255.0 # 将像素值归一化到 [0, 1] img = np.transpose(img, (2, 0, 1)) # 将图像从 HWC 格式转换为 CHW 格式 img = torch.from_numpy(img).float() # 将 Numpy 数组转换为 PyTorch 张量 img = img.unsqueeze(0) # 增加一个批量维度 return img def process_frame(model, img): img_preprocessed = preprocess_frame(img) results = model(img_preprocessed) # 处理模型的输出 results = results[0].detach().cpu().numpy() # 将结果从 GPU 移动到 CPU 并转换为 Numpy 数组 for x1, y1, x2, y2, conf, cls in results: # 将坐标从 [0, 1] 范围转换回图像的像素坐标 x1, y1, x2, y2 = x1 * img.shape[1], y1 * img.shape[0], x2 * img.shape[1], y2 * img.shape[0] # 在图像上画出边界框 cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (255, 0, 0), 2) # 在边界框旁边显示类别和置信度 cv2.putText(img, f'{int(cls)} {conf:.2f}', (int(x1), int(y1) - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2) # 显示图像 cv2.imshow('Tello with YOLOv5', img) return cv2.waitKey(1) def main(): tello = Tello() tello.connect() tello.streamon() frame_read = tello.get_frame_read() model = get_model() frame_skip = 2 # 每两帧处理一次 counter = 0 while True: if counter % frame_skip == 0: # 只处理每两帧中的一帧 img = frame_read.frame process_frame(model, img) counter += 1 cv2.destroyAllWindows() if __name__ == '__main__': main() 修改这段代码
import torch
from djitellopy import Tello
import cv2
import numpy as np
from models import yolo
def get_model():
# 假设 'yolov5s.yaml' 是 yolov5s 模型的定义文件的路径
model = yolo.Model('models/yolov5s.yaml')
checkpoint = torch.load('weights/yolov5s.pt')
model.load_state_dict(checkpoint['model'])
model.eval()
return model
def preprocess_frame(img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (640, 640)) # 将图像大小调整为模型的输入大小
img = img / 255.0 # 将像素值归一化到 [0, 1]
img = np.transpose(img, (2, 0, 1)) # 将图像从 HWC 格式转换为 CHW 格式
img = torch.from_numpy(img).float() # 将 Numpy 数组转换为 PyTorch 张量
img = img.unsqueeze(0) # 增加一个批量维度
return img
def process_frame(model, img):
img_preprocessed = preprocess_frame(img)
results = model(img_preprocessed)
# 处理模型的输出
results = results[0].detach().cpu().numpy() # 将结果从 GPU 移动到 CPU 并转换为 Numpy 数组
for x1, y1, x2, y2, conf, cls in results:
# 将坐标从 [0, 1] 范围转换回图像的像素坐标
x1, y1, x2, y2 = int(x1 * img.shape[3]), int(y1 * img.shape[2]), int(x2 * img.shape[3]), int(y2 * img.shape[2])
# 在图像上画出边界框
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
# 在边界框旁边显示类别和置信度
cv2.putText(img, f'{int(cls)} {conf:.2f}', (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
# 显示图像
cv2.imshow('Tello with YOLOv5', img)
return cv2.waitKey(1)
def main():
tello = Tello()
tello.connect()
tello.streamon()
frame_read = tello.get_frame_read()
model = get_model()
frame_skip = 1 # 每一帧处理一次
counter = 0
while True:
img = frame_read.frame
if counter % frame_skip == 0: # 只处理每一帧
process_frame(model, img)
counter += 1
if cv2.waitKey(1) & 0xFF == ord('q'): # 按下 'q' 键退出
break
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
阅读全文