Jupyter Notebook Python 多线程的一次实践

先说结论, 如果是silicon m1 的macbook的话会直接卡死 不建议这么做

首先我的代码如下

import cv2
import os
import glob
import numpy as np


def crop_persons(input_folder, output_folder, yolo_config, yolo_weights):
    net = cv2.dnn.readNet(yolo_weights, yolo_config)
    processed_images = []

    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    for root, dirs, files in os.walk(input_folder):
        for file in files:
            if file.lower().endswith('.jpg'):
                img_path = os.path.join(root,file)
                img = cv2.imread(img_path)
                print(f"Processing image: {img_path}")
                if img is None:
                    print(f"Failed to load image: {img_path}")
                    continue
                height, width, channels = img.shape

                blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
                net.setInput(blob)
                outs = net.forward(get_output_layers(net))

                for out in outs:
                    for detection in out:
                        scores = detection[5:]
                        class_id = np.argmax(scores)
                        confidence = scores[class_id]
                        if confidence > 0.3 and class_id == 0:  # class_id 0 is for 'person'
                            center_x = int(detection[0] * width)
                            center_y = int(detection[1] * height)
                            w = int(detection[2] * width)
                            h = int(detection[3] * height)

                            x = int(center_x - w / 2)
                            y = int(center_y - h / 2)

                            if x < 0 or y < 0 or x+w > width or y+h > height:
                                print(f"Invalid bounding box for image: {img_path}")
                                continue

                            person = img[y:y+h, x:x+w]
                            if person.size == 0:
                                print(f"Empty crop for image: {img_path}")
                                continue

                            # Create the same subdirectory structure in output_folder
                            relative_path = os.path.relpath(root, input_folder)
                            output_subfolder = os.path.join(output_folder, relative_path)
                            if not os.path.exists(output_subfolder):
                                os.makedirs(output_subfolder)

                            output_path = os.path.join(output_subfolder, os.path.basename(img_path))
                            cv2.imwrite(output_path, person)
                            # print(f"Saved cropped image to: {output_path}")

                            processed_images.append(img_path)
                    
    return processed_images 

def get_output_layers(net):
    layer_names = net.getLayerNames()
    return [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]


input_folder = 'output_frames'  # Replace with your image folder path
output_folder = 'output_yolo3'  # Replace with your desired output folder path
yolo_config = 'yolov3.cfg'  # Replace with the path to your YOLOv3 configuration file
yolo_weights = 'yolov3.weights'  # Replace with the path to your YOLOv3 weights file

crop_persons(input_folder, output_folder, yolo_config, yolo_weights)

这个是单进程的,我想要多进程的话 

然后jupyter中代码改成这样,需要满足全部三点

1. 功能函数不能放在外面, 需要创建一个py文件放在里面,然后引用

1. 引用系统路径,添加相关文件到系统路径

2. 外面的函数需要加上main 这一层封装(我自己测试时不带上main跑不了) 

from multiprocessing import Pool
import os
import sys
import sys
module_path = 'YourPath'
if module_path not in sys.path:
    sys.path.append(module_path)
import image_processing  # 导入自定义的 image_processing 模块

def process_images(input_folder, output_folder, yolo_config, yolo_weights):
    num_processes = os.cpu_count()  # 使用所有可用的 CPU 核心

    image_files = []
    for root, _, files in os.walk(input_folder):
        for file in files:
            if file.lower().endswith('.jpg'):
                img_path = os.path.join(root, file)
                image_files.append(img_path)

    args = [(img_path, output_folder, yolo_config, yolo_weights) for img_path in image_files]

    with Pool(num_processes) as pool:
        pool.map(image_processing.process_image, args)

if __name__ == "__main__":
    input_folder = 'output_frames'  # Replace with your image folder path
    output_folder = 'output_yolo3'  # Replace with your desired output folder path
    yolo_config = 'yolov3.cfg'  # Replace with the path to your YOLOv3 configuration file
    yolo_weights = 'yolov3.weights'  # Replace with the path to your YOLOv3 weights file

    process_images(input_folder, output_folder, yolo_config, yolo_weights)

运行起来之后就直接悲剧了, 电脑整个卡死,智能强制按电源键重启

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值