我试图找出为什么我的相机无法在 Windows 笔记本电脑上的 Pycharm 中打开。我的相机已经插入,并且代码(和相机代码)今天早些时候可以工作。当我运行这段代码时没有错误。我知道这一点,因为它确实打印了“输入形状:{img.shape}”。我感谢大家的帮助。这是我当前的代码:
class LiveStreamDataset(Dataset):
def __init__(self, transform=None, device=None):
self.generator = self.run_baslercamera()
self.transform = transform
self.device = device
def __getitem__(self, idx):
try:
image = next(self.generator)
except StopIteration:
self.generator = self.run_baslercamera()
image = next(self.generator)
if self.transform is not None:
# Convert the NumPy array to a PIL Image before transforming
image = Image.fromarray((image * 255).astype(np.uint8))
image = self.transform(image)
return image.to(self.device)
def __len__(self):
return int(1e6)
def run_baslercamera(self):
logger = logging.getLogger()
tl_factory = pylon.TlFactory.GetInstance()
devices = tl_factory.EnumerateDevices()
if len(devices) == 0:
logger.info("No cameras found!")
exit()
top_camera = pylon.InstantCamera(tl_factory.CreateDevice(devices[0]))
top_camera.Open()
converter = pylon.ImageFormatConverter()
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
top_camera.OutputQueueSize = 2
top_camera.StartGrabbing(pylon.GrabStrategy_LatestImages)
try:
while (True):
top_grabResult = top_camera.RetrieveResult(5000, pylon.TimeoutHandling_Return)
while not top_grabResult.GrabSucceeded():
logger.info("top grab failed, trying again")
top_grabResult = top_camera.RetrieveResult(5000, pylon.TimeoutHandling_Return)
if top_grabResult.GrabSucceeded():
image = converter.Convert(top_grabResult)
img = image.Array
img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
original_h = img.shape[0]
original_w = img.shape[1]
new_h = (1088 / 720) * (original_w)
offset = int(original_h - new_h)
img = img[offset:original_h, 0:original_w]
img = cv2.resize(img, (720, 1088))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img / 255.0
yield img
else:
logger.info(f"Top camera failed to grab an image")
yield np.zeros((1088, 720, 3))
finally:
top_camera.Close()
# Define transformation
transform = transforms.Compose([
transforms.Resize((640, 640)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# Initialize dataset
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dataset = LiveStreamDataset(transform=transform, device=device)
# Initialize dataloader
dataloader = DataLoader(dataset, batch_size=1, shuffle=False)
#camera code to open
def main(run=None, detection_args=None):
if detection_args.source == "1":
run_baslercamera(detection_args)
else:
run(**vars(detection_args))
def run_baslercamera(detection_args, device): # added device parameter here
logger = logging.getLogger()
tl_factory = pylon.TlFactory.GetInstance()
devices = tl_factory.EnumerateDevices()
if len(devices) == 0:
logger.info("No cameras found!")
exit()
top_camera = pylon.InstantCamera(tl_factory.CreateDevice(devices[0]))
top_camera.Open()
converter = pylon.ImageFormatConverter()
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
top_camera.OutputQueueSize = 2
top_camera.StartGrabbing(pylon.GrabStrategy_LatestImages)
try:
counter = 0
while (True):
top_grabResult = top_camera.RetrieveResult(5000, pylon.TimeoutHandling_Return)
while not top_grabResult.GrabSucceeded():
logger.info("top grab failed, trying again")
top_grabResult = top_camera.RetrieveResult(5000, pylon.TimeoutHandling_Return)
if top_grabResult.GrabSucceeded():
image = converter.Convert(top_grabResult)
img = image.Array
img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
original_h = img.shape[0]
original_w = img.shape[1]
new_h = (1088 / 720) * (original_w) # x/2160 = 1088/720 solve for x
offset = int(original_h - new_h)
img = img[offset:original_h, 0:original_w]
top_img = cv2.resize(img, (720, 1088))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img / 255.0
img = torch.from_numpy(img).permute(2, 0, 1).float().unsqueeze(0).to(device) # use device here
yield top_img # yield the image tensor
counter += 1
else:
logger.info(f"Top camera failed to grab an image")
top_img = np.zeros((1088, 720, 3))
top_img = cv2.resize(top_img, (0, 0), fx=0.5, fy=0.5)
yield top_img
finally:
top_camera.Close()
def detect_livestream(weights, source='1', img_size=640, conf_thres=0.5, iou_thres=0.5, device='', augment=False,
only_right_face=None):
set_logging()
device = select_device(device)
half = device.type != 'cpu'
# Make sure img_size is an even number
img_size = img_size if img_size % 2 == 0 else img_size + 1
# Load model
model = attempt_load(weights, map_location=device)
imgsz = check_img_size(img_size, s=model.stride.max())
if half:
model.half()
# Get colors
names = model.module.names if hasattr(model, 'module') else model.names
colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))]
# Dataloader
dataloader = DataLoader(dataset, batch_size=1, shuffle=False)
if source == '1':
for batch in dataloader:
img = batch
print(f"Input shape: {img.shape}")
# Inference (call model by passing image object, not path folder)
t1 = time_synchronized()
pred = model(img, augment=augment)[0] # Pass the image tensor directly to the model
# NMS
pred = non_max_suppression(pred, conf_thres, iou_thres)
t2 = time_synchronized()
# Process detections
for i, det in enumerate(pred): # detections per image
if det is not None and len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0s.shape).round()
# Write results
classifiedItems = [] # Create an empty list to store items with bounding boxes
for *xyxy, conf, cls in reversed(det):
label = f'{names[int(cls)]} {conf:.2f}'
# Update label to include package type
label = f'{names[int(cls)]} {conf:.2f}'
classifiedItems.append((xyxy, label, colors[int(cls)], conf)) # Add item details to the list
# Convert image data from (batch, channels, height, width) to (height, width, channels)
im0s = img.squeeze(0).cpu().numpy() # Remove the batch dimension and move to cpu
im0s = np.transpose(im0s, (1, 2, 0)) * 255 # Now we get (height, width, channels)
im0s = im0s.astype(np.uint8) # Convert to uint8
draw_border_boxes_Live(classifiedItems, im0s)
cv2.imshow('Live Stream', im0s) # display the window with the name 'Live Stream'
if cv2.waitKey(1) == ord('q'): # q to quit
break
if __name__ == '__main__':
weights = str(Path('../weights/best.pt'))
source = '1'
img_size = 640 # inference size (pixels)
conf_thres = 0.5 # object confidence threshold
iou_thres = 0.5
device = ''
augment = False
# Creating the detection_args namespace
detection_args = argparse.Namespace(weights=weights, source=source, img_size=img_size,
conf_thres=conf_thres, iou_thres=iou_thres,
device=device, augment=augment)
detect_livestream(weights, source, img_size, conf_thres, iou_thres, device, augment)