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
    评论
Jupyter Notebook中,内核中断通常是由于代码执行时间过长或者代码出现死循环等问题导致的。解决内核中断的方法有以下几种: 1. 重新启动内核:在Jupyter Notebook界面的菜单栏中选择"Kernel",然后选择"Restart"来重新启动内核。这会清除内核的状态并重新运行代码。 2. 检查代码:内核中断通常是由于代码执行时间过长或者出现死循环等问题导致的。可以检查代码是否存在无限循环、大量计算或者需要较长时间才能完成的操作。可以尝试优化代码,减少计算量或者使用更高效的算法。 3. 分块执行:如果代码执行时间过长,可以将代码分成多个块进行执行。这样可以避免一次性执行大量代码导致内核中断。可以使用Jupyter Notebook的"Cell"菜单中的"Run All Above"或者"Run All Below"来分块执行代码。 4. 增加超时时间:可以通过设置超时时间来避免代码执行时间过长导致的内核中断。可以在代码块中使用`%timeit`魔术命令来测量代码执行时间,并根据实际情况设置合适的超时时间。 5. 使用并行计算:如果代码需要进行大量计算,可以考虑使用并行计算来加速代码执行。可以使用Python多线程或者多进程库,如`concurrent.futures`、`multiprocessing`等来实现并行计算。 6. 更新软件包:有时内核中断可能是由于软件包版本不兼容或者存在bug导致的。可以尝试更新相关的软件包到最新版本,或者降级到稳定版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值