I am trying to calculate the cpu percentage a certain process take but the values are very differnt than that of the task manger

23 views Asked by At

i use psutil to calclate the cpu but i got weired results here is my code

import os
import numpy as np
import tensorflow as tf
from PIL import Image
import matplotlib
matplotlib.use('TkAgg')  # Use Tkinter backend for interaction
import matplotlib.pyplot as plt
from object_detection.utils import label_map_util
import cv2
start_cpu_usage = process.cpu_percent()
def detect_objects_in_images(model, folder_path, num_images=1):
    results = []
    # List all files in the folder
    files = os.listdir(folder_path)
    # Filter only image files
    image_files = [file for file in files if file.lower().endswith(('.png', '.jpg', '.jpeg'))]
    # Take only the first `num_images` from the list
    selected_image_files = image_files[:num_images]
    process = psutil.Process(os.getpid())
    for image_file in selected_image_files:
        image_path = os.path.join(folder_path, image_file)
        image_np = np.array(Image.open(image_path))
        print(f"Processing image: {image_path}")
        image = np.asarray(image_np)
        input_tensor = tf.convert_to_tensor(image)
        input_tensor = input_tensor[tf.newaxis, ...]
        
        cpu_percent = psutil.cpu_percent(interval=1)
        print(f"CPU Usage: {cpu_percent}%")
        time.sleep(1)
        output_dict = model(input_tensor)

        num_detections = int(output_dict.pop('num_detections'))
        output_dict = {key: value[0, :num_detections].numpy()
                       for key, value in output_dict.items()}
        output_dict['num_detections'] = num_detections
        output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)

        results.append({'image': image_np, 'detection_results': output_dict})
    return results

I tried changinge the intervl but still the task manger have 90 but psutil give me 12 % and very weird numbers

0

There are 0 answers