该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

python新手,做毕设用到,相关程序如下

file_num = g_file_num + int(

filename[filename.find(“_”)+1:filename.find(“.”)])

这一行报错:

Traceback (most recent call last):

File “F:\MATLAB\preprocess_cad60.py”, line 118, in

extract_images_and_matricize_keypoints()

File “F:\MATLAB\preprocess_cad60.py”, line 68, in extract_images_and_matricize_keypoints

filename[filename.find(“_”)+1:filename.find(“.”)])

ValueError: invalid literal for int() with base 10: ‘activityLabel’

就上面这部分。非常感谢大佬的帮助!!

如果需要看全部代码的话:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import os

import numpy as np

import pickle as pkl

import scipy.io as sio

import itertools

def remove_orientations_and_make_2d(joint_row):

“””As explained, this function removes all the orientation information and

makes each 3d pose 2d. It will also remove all the confidence bits from the

array”””

joint_row_without_orientations = []

for i in range(11):

del joint_row[:10]

joint_row_without_orientations.extend(joint_row[:3])

del joint_row[:4]

for i in range(4):

joint_row_without_orientations.extend(joint_row[:3])

del joint_row[:4]

joint_row_2d = []

joint_row_norm = []

for i in range(15):

x, y, z = joint_row_without_orientations[:3]

del joint_row_without_orientations[:3]

X = (156.8584456124928 + 0.0976862095248 * x – 0.0006444357104 * y

+ 0.0015715946682 * z)

Y = (125.5357201011431 + 0.0002153447766 * x – 0.1184874093530 * y

– 0.0022134485957 * z)

joint_row_2d.append([X,Y])

joint_row_norm.append([X/320, Y/240])#Normalized

return joint_row_2d, joint_row_norm

def extract_images_and_matricize_keypoints():

“”” As the title suggests the following function will modify the title of

each file to match that of the folder in which it lies and then will also

matricize the keypoint file associated with it “””

dat_dir = ‘data/cad60_dataset’

try:

joints = pkl.load(open(dat_dir+’/joints.p’, ‘rb’))

except:

joints = []

mat_joints = []

subdir_list = []

for filename in os.listdir(dat_dir):

if os.path.isdir(dat_dir+’/’+filename):

subdir_list.append(filename)

print subdir_list

if not os.path.exists(‘data/cad60_dataset/images’):

os.mkdir(‘data/cad60_dataset/images’)

g_file_num = 0

for subdir in subdir_list:

if not (subdir == ‘images’ or subdir == ‘mark’ or subdir == ‘crop’

or subdir == ‘joint’):

#Record the number of files (Depth OR RGB) in the folder

num_files_in_folder = int(len(os.listdir(dat_dir+’/’+subdir))/2)

print “Extracting files from dir: {}”.format(subdir)

print “Number of files in the current folder:{}”.format(

num_files_in_folder)

for filename in os.listdir(dat_dir+’/’+subdir):

file_num = g_file_num + int(

filename[filename.find(“_”)+1:filename.find(“.”)])

file_type = filename[0] #R(GB) or D

#Uncomment following line(and comment line below that one)

#if USING MULTIPLE DIRECTORIES from the CAD 60 dataset.

#os.rename(dat_dir+’/’+subdir+’/’+filename,

#dat_dir+’/images/’+subdir+file_num+’_’+file_type+’.jpg’)

os.rename(dat_dir+’/’+subdir+’/’+filename,

dat_dir+’/images/’+str(file_num)+’_’+file_type+’.jpg’)

print “Finished adding all files. Now extracting joint info”

with open(dat_dir+’/’+subdir+’.txt’, ‘rU’) as txt_file:

lines_list = txt_file.read()

lines_list = lines_list.split(‘\n’)[:-1]

for line in lines_list:

joint_row = line.split(‘,’)[:-1]

if joint_row:

try:

joint_row[1:] = (

[float(joint_str) for joint_str in joint_row[1:]])

except ValueError:

print “[ERROR] Could not convert string to float”

print joint_row

return

joint_row_norm = list(joint_row)

joint_row[1:], joint_row_norm[1:] = (

remove_orientations_and_make_2d(joint_row[1:]))

#MATLAB Stuff

chain_mix = list(joint_row_norm)

chain_mix[0] = [g_file_num + int(chain_mix[0])]

chain = [item for sublist in chain_mix for item in sublist]

mat_joints.append(chain)

#Pickle Stuff

#joint_row[0] = subdir+joint_row[0]#UNCOMMENT if multiple

#directories of CAD60 dataset

joint_row[0] = str(chain_mix[0][0])#Since chain_mix[0] is a list

joints.append(joint_row)

else:

continue

print “Joint number is at {} = Number of files handled?”.format(

chain_mix[0])

g_file_num += num_files_in_folder

os.rmdir(dat_dir+’/’+subdir)

print “Number of files handled: {}”.format(g_file_num)

pkl.dump(joints, open(dat_dir+’/joints.p’, ‘wb’))

#sio.savemat(dat_dir+’/joints.mat’, {‘joints’:mat_joints})#Store as mat

if __name__ == ‘__main__’:

extract_images_and_matricize_keypoints()