当前位置: 首页 > news >正文

专门做网站的公司与外包公司有哪些北京做网站建设的公司哪家好

专门做网站的公司与外包公司有哪些,北京做网站建设的公司哪家好,wordpress怎么换头像不显示不出来,搜狐快站装修网站建设作者:CSDN _养乐多_ 本文将介绍如何实现一个可视化图片和标签信息的查看器,代码使用python实现。点击下一张和上一张可以切换图片。 文章目录 一、脚本界面二、完整代码 一、脚本界面 界面如下图所示, 二、完整代码 使用代码时&#xff0…

作者:CSDN @ _养乐多_

本文将介绍如何实现一个可视化图片和标签信息的查看器,代码使用python实现。点击下一张和上一张可以切换图片。

在这里插入图片描述


文章目录

      • 一、脚本界面
      • 二、完整代码


一、脚本界面

界面如下图所示,

在这里插入图片描述

二、完整代码

使用代码时,需要修改 class_id_to_name 还有 YOLO 格式的图片(images)文件夹路径和标签(labels)文件夹路径。

from PIL import Image, ImageDraw, ImageFont, ImageTk
import tkinter as tk
from tkinter import ttk
import os# 创建类别 ID 到中文名称的映射
class_id_to_name = {0: "飞机",1: "船只",2: "储油罐",3: "棒球场",4: "网球场",5: "篮球场",6: "跑道场地",7: "港口",8: "桥梁",9: "车辆"
}def get_image_size(image_path):# 打开图片文件with Image.open(image_path) as img:# 获取图片的宽度和高度width, height = img.sizereturn width, heightdef read_yolo_labels(label_file, img_width, img_height):with open(label_file, 'r') as file:lines = file.readlines()boxes = []for line in lines:parts = line.strip().split()class_id = int(parts[0])x_center = float(parts[1])y_center = float(parts[2])width = float(parts[3])height = float(parts[4])# 将 YOLO 格式转换为像素坐标x_center_px = int(x_center * img_width)y_center_px = int(y_center * img_height)width_px = int(width * img_width)height_px = int(height * img_height)# 计算矩形框的左上角和右下角点x1 = int(x_center_px - width_px / 2)y1 = int(y_center_px - height_px / 2)x2 = int(x_center_px + width_px / 2)y2 = int(y_center_px + height_px / 2)boxes.append((x1, y1, x2, y2, class_id))return boxesdef draw_boxes_on_image(image_path, boxes):# 使用 PIL 加载图片img = Image.open(image_path)draw = ImageDraw.Draw(img)# 定义颜色和线宽box_color = "yellow"  # 选择一个亮色line_width = 5  # 设置较粗的线宽# 使用支持中文字符的系统字体try:# 尝试使用支持中文的常见系统字体font = ImageFont.truetype("msyh.ttc", size=24)  # 微软雅黑except IOError:# 回退到默认字体font = ImageFont.load_default()for (x1, y1, x2, y2, class_id) in boxes:# 绘制矩形框draw.rectangle([x1, y1, x2, y2], outline=box_color, width=line_width)# 从 class_id 获取类别名称class_name = class_id_to_name.get(class_id, "未知")text = class_nametext_width, text_height = 50, 40  # 设定文本框的宽度和高度text_x = x1text_y = y1 - text_height - 5# 绘制带背景矩形的文本draw.rectangle([text_x, text_y, text_x + text_width, text_y + text_height], fill=box_color)draw.text((text_x, text_y), text, fill="black", font=font)return imgdef display_image_with_boxes(image_file, label_file):# 获取图片尺寸img_width, img_height = get_image_size(image_file)# 读取 YOLO 标签boxes = read_yolo_labels(label_file, img_width, img_height)# 在图片上绘制矩形框img_with_boxes = draw_boxes_on_image(image_file, boxes)return img_with_boxesclass ImageViewer:def __init__(self, root, image_files, label_files):self.root = rootself.image_files = image_filesself.label_files = label_filesself.current_index = 0# 设置固定的查看器大小self.viewer_width = 800self.viewer_height = 600# 初始化界面self.init_ui()def init_ui(self):self.canvas = tk.Canvas(self.root, width=self.viewer_width, height=self.viewer_height)self.canvas.pack()self.prev_button = ttk.Button(self.root, text="上一张", command=self.prev_image)self.prev_button.pack(side=tk.LEFT)self.next_button = ttk.Button(self.root, text="下一张", command=self.next_image)self.next_button.pack(side=tk.RIGHT)self.update_image()def update_image(self):image_file = self.image_files[self.current_index]label_file = self.label_files[self.current_index]img_with_boxes = display_image_with_boxes(image_file, label_file)# 将图片转换为 Tkinter 可用格式img_with_boxes = img_with_boxes.convert("RGB")img_tk = ImageTk.PhotoImage(img_with_boxes)# 计算缩放比例img_width, img_height = img_with_boxes.sizescale = min(self.viewer_width / img_width, self.viewer_height / img_height)new_width = int(img_width * scale)new_height = int(img_height * scale)# 缩放图片img_resized = img_with_boxes.resize((new_width, new_height), Image.Resampling.LANCZOS)img_tk_resized = ImageTk.PhotoImage(img_resized)# 清除画布上的内容self.canvas.delete("all")# 在画布上显示图片self.canvas.create_image(self.viewer_width / 2, self.viewer_height / 2, image=img_tk_resized)# 保持对图像的引用self.canvas.image = img_tk_resizeddef prev_image(self):if self.current_index > 0:self.current_index -= 1self.update_image()def next_image(self):if self.current_index < len(self.image_files) - 1:self.current_index += 1self.update_image()if __name__ == "__main__":# 图片和标签文件的路径image_folder = 'E:\\DataSet\\positive'label_folder = 'E:\\DataSet\\yolo_labels'# 获取所有图片和标签文件image_files = sorted([os.path.join(image_folder, f) for f in os.listdir(image_folder) if f.endswith('.jpg')])label_files = sorted([os.path.join(label_folder, f) for f in os.listdir(label_folder) if f.endswith('.txt')])# 创建 Tkinter 窗口root = tk.Tk()root.title("图片标注查看器")# 启动图像查看器viewer = ImageViewer(root, image_files, label_files)root.mainloop()
http://www.yayakq.cn/news/541541/

相关文章:

  • 太原谁家网站做的好改wordpress的wp_admin
  • 灵宝网站建设商品展示型网站有哪些
  • 培训中心网站建设论文广州做网站报价
  • 一般网站建设公司怎么收费linux做网站方便吗
  • 网站开发需要什么人员网站开发代理商
  • 口碑好的定制网站建设网页设计与网站开发第三版课后答案
  • 工程建筑公司网站长兴县住房和城乡建设局网站
  • 网站制作最陇南市城乡建设局网站
  • 网站建设 工作方案国外免实名域名
  • 湖南常德广宇建设网站高档vi设计公司
  • 自己建个网站多少钱wordpress主题 SEO优化
  • 做网站学习什么仿站网站建设
  • 文山知名网站建设哪家好网站信息发布系统
  • 墨尔本网站建设电子商务师证官网
  • 哪个网站做汽车保养比较好微分销系统定制专家
  • 直播是网站怎么做网站建设注册什么公司
  • 宁波住房城乡建设局网站群晖搭建wordpress修改固定链接
  • 门户网站收录互联网挣钱新方法
  • 常州制作网站信息制作网站需要多少钱
  • 国内知名设计网站建设银行天津招聘网站
  • 网站搭建好显示建设中wordpress怎么接入支付
  • 男子做网站程序员代做网站违法
  • 导航在左侧的网站欣赏wordpress tag文件
  • 网站 网络推广员工oa系统
  • 中国行业网站大全公司建设网站申请报告
  • 河北大城县网站建设公司公司建设网站哪家好
  • 重庆网站建设 沛宣网页生成视频
  • 织梦模板国外网站上海市企业登记网络服务平台
  • 时尚网站的建设策划天津seo优化公司哪家好
  • 佛山出格建站汕头网站建设培训公司