import numpy as np import cv2 import os from time import sleep from PIL import Image…
- User
import numpy as np import cv2 import os from time import sleep from PIL import Image import torch
Install necessary libraries
Make sure to install YOLOv5 dependencies
pip install opencv-python pillow torch
class WindowCapture: def init(self, window_name): self.window_name = window_name self.capture = cv2.VideoCapture(0)
def get_screenshot(self): ret, frame = self.capture.read() if not ret: raise Exception('Failed to capture frame from camera.') return frame def generate_image_dataset(self): if not os.path.exists("images"): os.mkdir("images") while True: img = self.get_screenshot() im = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) im.save(f"./images/img_{len(os.listdir('images'))}.jpeg") sleep(1) def get_window_size(self): w = int(self.capture.get(3)) h = int(self.capture.get(4)) return w, hclass ImageProcessor: def init(self, img_size, weights_file_path): self.model = self.load_yolov5(weights_file_path) self.model.to('cuda' if torch.cuda.is_available() else 'cpu') self.model.eval()
self.W, self.H = img_size def load_yolov5(self, weights_file_path): model = torch.hub.load('ultralytics/yolov5:v5.0', 'yolov5s', pretrained=False) checkpoint = torch.load(weights_file_path, map_location='cuda' if torch.cuda.is_available() else 'cpu') model.load_state_dict(checkpoint['model']) return model def process_image(self, img): results = self.model(img) pred = results.xyxy[0].cpu().numpy() # Convert coordinates to match the original image size pred[:, [0, 2]] *= self.W pred[:, [1, 3]] *= self.H return pred def draw_identified_objects(self, img, coordinates): for coordinate in coordinates: x, y, w, h, conf, cls = coordinate color = (0, 255, 0) # Green color for bounding box cv2.rectangle(img, (int(x), int(y)), (int(w), int(h)), color, 2) cv2.putText(img, f'Class: {int(cls)}, Conf: {conf:.2f}', (int(x), int(y) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) cv2.imshow('window', img)Set your game window name
window_name = "Superfighters Deluxe v1.3.7d"
Provide the full path to your custom weights file
weights_file_path = r"C:\Users\Ninja.000\Downloads\exp3-20240102T094124Z-001\exp3\weights\best.pt"
wincap = WindowCapture(window_name) improc = ImageProcessor(wincap.get_window_size(), weights_file_path)
while True: ss = wincap.get_screenshot()
if cv2.waitKey(1) == ord('q'): cv2.destroyAllWindows() break # Preprocess image for YOLOv5 img = wincap.get_screenshot() img = cv2.resize(img, (640, 480)) img = img.transpose(2, 0, 1) # Channels first (C, H, W) img = np.expand_dims(img, 0) # Add batch dimension # Convert to torch tensor img = torch.from_numpy(img).float().div(255.0).unsqueeze(0) # Inference and post-process coordinates = improc.process_image(img) # Draw bounding boxes on the image improc.draw_identified_objects(ss, coordinates) for coordinate in coordinates: print(coordinate) print() sleep(0.2)print('Finished.')
C:\Users\Ninja.000\Downloads\Things>python aa.py Using cache found in C:\Users\Ninja.000/.cache\torch\hub\ultralytics_yolov5_v5.0 Traceback (most recent call last): File "C:\Users\Ninja.000\Downloads\Things\aa.py", line 80, in improc = ImageProcessor(wincap.get_window_size(), weights_file_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\Downloads\Things\aa.py", line 41, in init self.model = self.load_yolov5(weights_file_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\Downloads\Things\aa.py", line 48, in load_yolov5 model = torch.hub.load('ultralytics/yolov5:v5.0', 'yolov5s', pretrained=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\hub.py", line 566, in load model = _load_local(repo_or_dir, model, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\hub.py", line 592, in _load_local hub_module = _import_module(MODULE_HUBCONF, hubconf_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\hub.py", line 106, in _import_module spec.loader.exec_module(module) File "", line 936, in exec_module File "", line 1073, in get_code File "", line 1130, in get_data FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\Ninja.000/.cache\torch\hub\ultralytics_yolov5_v5.0\hubconf.py'
- Assistant
The error you're encountering suggests that the
hubconf.pyfile cannot be found within the specified directory (in this case, the cache directory for Torch Hub). This might be happening because the YOLOv5 repository cannot be successfully cloned from GitHub, or because there's another issue with the setup of Torch Hub.To resolve this issue, follow these steps:
-
Make sure you have an internet connection and access to GitHub, as
torch.hub.loadwill attempt to download the YOLOv5 repository. -
Ensure
gitis installed on your system and that it's accessible from your command line or terminal.torch.hub.loadusesgitto clone repositories. -
You might also want to clear the cache of Torch Hub to remove any potentially corrupted files that have been downloaded. You can do this by removing the contents of
C:\Users\Ninja.000/.cache\torch\hub\. -
Alternatively, you can manually clone the YOLOv5 repository into your working directory using git and then point your YOLOv5 loading mechanism to the locally cloned repository. Here's how you could do it:
git clone https://github.com/ultralytics/yolov5.gitThen, in your Python code, modify the
load_yolov5method to load the model from a local directory:def load_yolov5(self, local_dir_path, weights_file_path): # Load the model from a local directory instead of using torch.hub model = torch.hub.load(local_dir_path, 'custom', path=weights_file_path, source='local') return modelMake sure to pass a correct
local_dir_pathwhen you call this function.-
In your given code, you seem to try to load a custom-trained YOLOv5 model. Double-check that
weights_file_pathpoints to the correct location of your.ptfile. -
If you continue to face issues with Torch Hub, you can directly load the YOLOv5 model from your local file system without using Torch Hub altogether, but this would require you to handle the model architecture manually.
Lastly, make sure you're running your code with appropriate permissions and that nothing is blocking your script's access to the required directories and files. If your Python environment is installed in a system-wide directory (like Program Files), you might want to consider using a virtual environment to avoid permission issues.
-
- User
import numpy as np import cv2 import os from time import sleep from PIL import Image import torch
Set your game window name
window_name = "Superfighters Deluxe v1.3.7d"
Provide the full path to your custom weights file
weights_file_path = r"C:\Users\Ninja.000\Downloads\exp3-20240102T094124Z-001\exp3\weights\best.pt"
local_dir_path = r"C:\Users\Ninja.000\Downloads\yolov5-master\yolov5-master"
class WindowCapture: def init(self, window_name): self.window_name = window_name self.capture = cv2.VideoCapture(0)
def get_screenshot(self): ret, frame = self.capture.read() if not ret: raise Exception('Failed to capture frame from camera.') return frame def generate_image_dataset(self): if not os.path.exists("images"): os.mkdir("images") while True: img = self.get_screenshot() im = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) im.save(f"./images/img_{len(os.listdir('images'))}.jpeg") sleep(1) def get_window_size(self): w = int(self.capture.get(3)) h = int(self.capture.get(4)) return w, hclass ImageProcessor: def init(self, img_size, local_dir_path, weights_file_path): self.model = self.load_yolov5(local_dir_path, weights_file_path) self.model.to('cuda' if torch.cuda.is_available() else 'cpu') self.model.eval()
self.W, self.H = img_size def load_yolov5(self, local_dir_path, weights_file_path): # Load the model from a local directory instead of using torch.hub model = torch.hub.load(local_dir_path, 'custom', path=weights_file_path, source='local') return model def process_image(self, img): results = self.model(img) pred = results.xyxy[0].cpu().numpy() # Convert coordinates to match the original image size pred[:, [0, 2]] *= self.W pred[:, [1, 3]] *= self.H return pred def draw_identified_objects(self, img, coordinates): for coordinate in coordinates: x, y, w, h, conf, cls = coordinate color = (0, 255, 0) # Green color for bounding box cv2.rectangle(img, (int(x), int(y)), (int(w), int(h)), color, 2) cv2.putText(img, f'Class: {int(cls)}, Conf: {conf:.2f}', (int(x), int(y) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) cv2.imshow('window', img)wincap = WindowCapture(window_name) improc = ImageProcessor(wincap.get_window_size(), local_dir_path, weights_file_path)
while True: ss = wincap.get_screenshot()
if cv2.waitKey(1) == ord('q'): cv2.destroyAllWindows() break # Preprocess image for YOLOv5 img = wincap.get_screenshot() img = cv2.resize(img, (640, 480)) img = img.transpose(2, 0, 1) # Channels first (C, H, W) img = np.expand_dims(img, 0) # Add batch dimension # Convert to torch tensor img = torch.from_numpy(img).float().div(255.0) # Inference and post-process coordinates = improc.process_image(img) # Draw bounding boxes on the image improc.draw_identified_objects(ss, coordinates) for coordinate in coordinates: print(coordinate) print() sleep(0.2)print('Finished.')
C:\Users\Ninja.000\Downloads\Things>python aa.py YOLOv5 2024-1-1 Python-3.11.7 torch-2.1.2+cpu CPU
Fusing layers... Model summary: 157 layers, 7015519 parameters, 0 gradients, 15.8 GFLOPs Adding AutoShape... Traceback (most recent call last): File "C:\Users\Ninja.000\Downloads\Things\aa.py", line 96, in coordinates = improc.process_image(img) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\Downloads\Things\aa.py", line 57, in process_image results = self.model(img) ^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\utils_contextlib.py", line 115, in decorate_context return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\Downloads\yolov5-master\yolov5-master\models\common.py", line 688, in forward return self.model(ims.to(p.device).type_as(p), augment=augment) # inference ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Ninja.000\Downloads\yolov5-master\yolov5-master\models\common.py", line 520, in forward b, ch, h, w = im[0].shape # batch, channel, height, width ^^^^^^^^^^^ ValueError: not enough values to unpack (expected 4, got 3)
- Assistant
The error you're seeing,
ValueError: not enough values to unpack (expected 4, got 3), indicates that theforwardmethod is expecting a four-dimensional tensor representing (batch_size, channels, height, width), but it is only receiving a three-dimensional tensor (channels, height, width).This occurs because you've mistakenly removed the batch dimension when you made this change:
# Convert to torch tensor img = torch.from_numpy(img).float().div(255.0)You should reintroduce the batch dimension to match the expected dimensions. By using
unsqueeze(0), you introduce a batch dimension with size 1, which is what the model expects:# Convert to torch tensor, including the batch dimension img = torch.from_numpy(img).float().div(255.0).unsqueeze(0)Undo the change by restoring this line to reintroduce the batch dimension. Once you correct that, your code should run without the relevant
ValueError. However, if you're running an infinite loop or a loop without an exit condition, you should also ensure that there is a way to break out of it using specific conditions or keyboard interrupts. In your current setup, pressing 'q' should terminate the loop according to:if cv2.waitKey(1) == ord('q'): cv2.destroyAllWindows() breakEnsure your window is focused when pressing 'q' to exit; otherwise, the keypress may not be detected.