{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":19989,"databundleVersionId":1160143,"sourceType":"competition"},{"sourceId":11196081,"sourceType":"datasetVersion","datasetId":6989938}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import cv2\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-03-28T13:18:58.885482Z","iopub.execute_input":"2025-03-28T13:18:58.886509Z","iopub.status.idle":"2025-03-28T13:19:00.084710Z","shell.execute_reply.started":"2025-03-28T13:18:58.886457Z","shell.execute_reply":"2025-03-28T13:19:00.083814Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def labels(path_to_file, image_width, image_height):\n    \"\"\"\n    读取YOLO格式的标签文件，并转换为 OpenCV 矩形框格式 (x1, y1, x2, y2)。\n\n    参数:\n    path_to_file (str): YOLO标注的txt文件路径\n    image_width (int): 原始图像的宽度\n    image_height (int): 原始图像的高度\n\n    返回:\n    list: [(class_id, x1, y1, x2, y2), ...]\n    \"\"\"\n    \n    # 读取YOLO格式的txt文件 (空格分隔)\n    df = pd.read_csv(path_to_file, delimiter=\" \", header=None, names=[\"class_id\", \"x_center\", \"y_center\", \"width\", \"height\"])\n    df=df.drop(df['class_id'])\n    # 计算 OpenCV 矩形框格式 (x1, y1, x2, y2)\n    df[\"x1\"] = ((df[\"x_center\"] - df[\"width\"] / 2) * image_width).astype(int)\n    df[\"y1\"] = ((df[\"y_center\"] - df[\"height\"] / 2) * image_height).astype(int)\n    df[\"x2\"] = ((df[\"x_center\"] + df[\"width\"] / 2) * image_width).astype(int)\n    df[\"y2\"] = ((df[\"y_center\"] + df[\"height\"] / 2) * image_height).astype(int)\n\n    # 返回转换后的数据 [(class_id, x1, y1, x2, y2), ...]\n    return df[[ \"x1\", \"y1\", \"x2\", \"y2\"]].values.tolist()\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T13:19:37.766135Z","iopub.execute_input":"2025-03-28T13:19:37.766541Z","iopub.status.idle":"2025-03-28T13:19:37.773509Z","shell.execute_reply.started":"2025-03-28T13:19:37.766510Z","shell.execute_reply":"2025-03-28T13:19:37.772435Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\ndef show(picture_name='00333207f.jpg'):\n    root_picture_path='/kaggle/input/global-wheat-detection/train/'\n    root_label_path='/kaggle/input/labels/labels'\n    \n    path_to_picture_file=os.path.join(root_picture_path,picture_name)\n    path_to_label_file=os.path.join(root_label_path,picture_name.split('.')[0]+'.txt')\n    \n    image=cv2.imread(path_to_picture_file)\n    # 假设有多个框的坐标 (x1, y1, x2, y2)\n    image_width=1024\n    image_height=1024\n    boxes = labels(path_to_label_file, image_width, image_height)\n    \n    for (x1, y1, x2, y2) in boxes:\n        cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2)\n    \n    \n    \n    import matplotlib.pyplot as plt\n    \n    # OpenCV 读取的图像是 BGR，需要转换为 RGB\n    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    \n    plt.imshow(image_rgb)\n    plt.axis(\"off\")  # 隐藏坐标轴\n    plt.show()\n\n\nshow()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T13:19:40.437422Z","iopub.execute_input":"2025-03-28T13:19:40.437758Z","iopub.status.idle":"2025-03-28T13:19:40.917799Z","shell.execute_reply.started":"2025-03-28T13:19:40.437733Z","shell.execute_reply":"2025-03-28T13:19:40.916717Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nfrom collections import Counter\nimport matplotlib.pyplot as plt\nroot='/kaggle/input/labels/labels'\nlist_file=os.listdir(root)\nbox_num_list=[]\nfor filename in list_file:\n    path=os.path.join(root,filename)\n    df = pd.read_csv(path, delimiter=\" \",header=None)\n    if(df.iloc[:,4]>0.5).any():\n        show(filename.split('.')[0]+'.jpg')\n    \n\n    if(df.iloc[:,3]>0.5).any():\n        show(filename.split('.')[0]+'.jpg')\n    #print(df)\n    num_box=len(df)\n    if(num_box>160):\n        show(filename.split('.')[0]+'.jpg')\n    box_num_list.append(num_box)\n    \n\n\n# 使用Counter统计每个数字出现的次数\ncount = Counter(box_num_list)\n\n# 获取数字和它们的计数\nnumbers = list(count.keys())\ncounts = list(count.values())\n\n# 绘制柱状图\nplt.bar(numbers, counts)\n\n# 添加标题和标签\nplt.title('BOX Frequency')\nplt.xlabel('Number of Box')\nplt.ylabel('Count')\n\n# 显示图形\nplt.show()\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T13:30:12.594852Z","iopub.execute_input":"2025-03-28T13:30:12.595237Z","iopub.status.idle":"2025-03-28T13:30:22.860522Z","shell.execute_reply.started":"2025-03-28T13:30:12.595206Z","shell.execute_reply":"2025-03-28T13:30:22.859474Z"}},"outputs":[],"execution_count":null}]}