json转yolo出现错误

你好,我在使用你的yolov8实例分割训练自己的数据集这一教程进行json转化yolo时出现了以下报错

  0%|          | 1/282 [00:00<00:00, 921.62it/s]
Traceback (most recent call last):
  File "C:\Github\ultralytics-main\json2txt.py", line 53, in <module>
    convert_label_json(json_dir, save_dir, classes)
  File "C:\Github\ultralytics-main\json2txt.py", line 16, in convert_label_json
    json_dict = json.load(load_f)
  File "C:\anaconda\envs\yolo8_truck\lib\json\__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\anaconda\envs\yolo8_truck\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\anaconda\envs\yolo8_truck\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\anaconda\envs\yolo8_truck\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Process finished with exit code 1

请问以下这是什么问题呢

json文件的格式不对还是编码有问题吧,导致读取json失败了

  • 这篇博客: 将yolov4预测结果保存为json文件中的 yolo.py 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • def save_json(self,image,file_name,image_id,categories):
        image_shape = np.array(np.shape(image)[0:2])
            
        if self.letterbox_image:
            crop_img = np.array(letterbox_image(image, (self.model_image_size[1],self.model_image_size[0])))
        else:
            crop_img = image.convert('RGB')
            crop_img = crop_img.resize((self.model_image_size[1],self.model_image_size[0]), Image.BICUBIC)
        photo = np.array(crop_img,dtype = np.float32) / 255.0
        photo = np.transpose(photo, (2, 0, 1))
        
        images = [photo]
        
        with torch.no_grad():
            images = torch.from_numpy(np.asarray(images))
            if self.cuda:
                images = images.cuda()
    
            outputs = self.net(images)
            output_list = []
            for i in range(3):
                output_list.append(self.yolo_decodes[i](outputs[i]))
    
            
            output = torch.cat(output_list, 1)
            batch_detections = non_max_suppression(output, len(self.class_names),
                                                    conf_thres=self.confidence,
                                                    nms_thres=self.iou)
            
            try:
                batch_detections = batch_detections[0].cpu().numpy()
            except:
                return []
            
            top_index = batch_detections[:,4] * batch_detections[:,5] > self.confidence
            top_conf = batch_detections[top_index,4]*batch_detections[top_index,5]
            top_label = np.array(batch_detections[top_index,-1],np.int32)
            top_bboxes = np.array(batch_detections[top_index,:4])
            top_xmin, top_ymin, top_xmax, top_ymax = np.expand_dims(top_bboxes[:,0],-1),np.expand_dims(top_bboxes[:,1],-1),np.expand_dims(top_bboxes[:,2],-1),np.expand_dims(top_bboxes[:,3],-1)
    
            if self.letterbox_image:
                boxes = yolo_correct_boxes(top_ymin,top_xmin,top_ymax,top_xmax,np.array([self.model_image_size[0],self.model_image_size[1]]),image_shape)
            else:
                top_xmin = top_xmin / self.model_image_size[1] * image_shape[1]
                top_ymin = top_ymin / self.model_image_size[0] * image_shape[0]
                top_xmax = top_xmax / self.model_image_size[1] * image_shape[1]
                top_ymax = top_ymax / self.model_image_size[0] * image_shape[0]
                boxes = np.concatenate([top_ymin,top_xmin,top_ymax,top_xmax], axis=-1)
                
        font = ImageFont.truetype(font='model_data/simhei.ttf',size=np.floor(3e-2 * np.shape(image)[1] + 0.5).astype('int32'))
    
        thickness = max((np.shape(image)[0] + np.shape(image)[1]) // self.model_image_size[0], 1)
        content_json=[]
        for i, c in enumerate(top_label):
            predicted_class = self.class_names[c]
            score = top_conf[i]
    
            top, left, bottom, right = boxes[i]
            w=right-left
            h=bottom-top
    
            y = float(max(0, top))
            x = float(max(0, left))
            w = float(max(0, w))
            h = float(max(0, h))
            score = float(score)
            bbox=[x,y,w,h]
    
            # print(label, top, left, bottom, right)
            cat_id=categories[c]['id']
    	    content_dic = {
    	        "image_id":image_id,
    	        "category_id": cat_id,
    	        "bbox": bbox,
    	        "score": score
    	    }
        	content_json.append(content_dic)   
        return content_json