Guest User

check_images

a guest
Apr 9th, 2018
1,881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. import csv
  2. import cv2
  3. import os
  4. import numpy as np
  5.  
  6. FOLDER = 'test'
  7. CSV_FILE = 'test.csv'
  8.  
  9. with open(CSV_FILE, 'r') as fid:
  10.    
  11.     print('Checking file:', CSV_FILE, 'in folder:', FOLDER)
  12.    
  13.     file = csv.reader(fid, delimiter=',')
  14.     first = True
  15.    
  16.     cnt = 0
  17.     error_cnt = 0
  18.     error = False
  19.     for row in file:
  20.         if error == True:
  21.             error_cnt += 1
  22.             error = False
  23.            
  24.         if first == True:
  25.             first = False
  26.             continue
  27.        
  28.         cnt += 1
  29.        
  30.         name, width, height, xmin, ymin, xmax, ymax = row[0], int(row[1]), int(row[2]), int(row[4]), int(row[5]), int(row[6]), int(row[7])
  31.        
  32.         path = os.path.join(FOLDER, name)
  33.         img = cv2.imread(path)
  34.        
  35.        
  36.         if type(img) == type(None):
  37.             error = True
  38.             print('Could not read image', img)
  39.             continue
  40.        
  41.         org_height, org_width = img.shape[:2]
  42.        
  43.         if org_width != width:
  44.             error = True
  45.             print('Width mismatch for image: ', name, width, '!=', org_width)
  46.        
  47.         if org_height != height:
  48.             error = True
  49.             print('Height mismatch for image: ', name, height, '!=', org_height)
  50.        
  51.         if xmin > org_width:
  52.             error = True
  53.             print('XMIN > org_width for file', name)
  54.            
  55.         if xmax > org_width:
  56.             error = True
  57.             print('XMAX > org_width for file', name)
  58.        
  59.         if ymin > org_height:
  60.             error = True
  61.             print('YMIN > org_height for file', name)
  62.        
  63.         if ymax > org_height:
  64.             error = True
  65.             print('YMAX > org_height for file', name)
  66.        
  67.         if error == True:
  68.             print('Error for file: %s' % name)
  69.             print()
  70.        
  71.     print('Checked %d files and realized %d errors' % (cnt, error_cnt))
Advertisement
Add Comment
Please, Sign In to add comment