Problems in Python

0

Hello, how are you? I was using Python a few days ago and had no problem, yesterday I wanted to modify my code, I had a problem when compiling: UnicodeDecodeError: 'ascii' codec can not decode byte 0xf3 in position 30: ordinal not in range (128)

From what I've read they say it's a problem of accents or eñes, in my program there's nothing like that, now I copy it so they can see it, I need your help ... I'm desperate

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

"""
Created on Wed Dec 13 17:41:56 2017

@author: pc
"""



# when ddepth=-1, the output image will have the same depth as the source
# cv2.CV_64F

from matplotlib import pyplot as plt
import matplotlib.cm as cm
import matplotlib.patches as patches

import numpy as np
import cv2

from Class_2D_DoG import DoG_2D
from func_Custom import (Convert255ToBalanced, ConvertBalancedTo255)

import os
import sys
import scipy.io


#Ploting Settings
m=0.5
graph_size = (m*19.2, m*10.8)
graph_dpi = 300

if __name__ == "__main__":

#==============================================================================
#   Setting the DoG Filter
#==============================================================================

    #Set DoG Filter Parameters
    Rc = 1.5
    rR = 2.5
    rV = 0.95


    #Create Class: DoG Filter
    DoG = DoG_2D(Rc=Rc, rR=rR, rV=rV)

#==============================================================================
#   Setting First Derivative Filter    
#==============================================================================
    #First Derivate Filter: Horizontal and Veritical Directions
    dx1 = np.array([[0,0,0],[-1.0,0,1.0],[0,0,0]])
    dy1 = np.array([[0,0,0],[-1.0,0,1.0],[0,0,0]]).T

#==============================================================================
#   Setting the Folder
#==============================================================================
    #Set the folder
    main_path = os.path.dirname(sys.argv[0]) 
    pupil_name = 'p3'
    img_name = 'Flores'    

    #Get file names from AD-
    data_path_neg = os.path.join(main_path, pupil_name, img_name,'AD-')
    AD_neg  = os.listdir(data_path_neg)
    AD_neg  = AD_neg[0:13]
    AD_neg  = AD_neg[::-1]

    #Get file names from AD+
    data_path_pos = os.path.join(main_path, pupil_name, img_name, 'AD+')
    AD_pos  = os.listdir(data_path_pos)
    AD_pos  = AD_pos[0:13]

#==============================================================================
#   Processing Loop 
#==============================================================================
    n = 25
    AD = np.linspace(-3.0,+3.0, n)  
    Focus_DoG = np.zeros(n)
    Focus_D1 = np.zeros(n)

    for i in range(0,n):
        if i < 12:
            mat = scipy.io.loadmat(data_path_neg + '//' + AD_neg[i])  
            imgIn = mat['imag']
        else:
            mat = scipy.io.loadmat(data_path_pos + '//' + AD_pos[i-12])  
            imgIn = mat['imag']



        #Convert input image from [0...128...255] to [-1...0...+1] (Optional)
#        imgIn = Convert255ToBalanced(imgIn)
#        imgIn = ConvertBalancedTo255(imgIn)

        #Calculating: DoG Output
        imgOut = cv2.filter2D(imgIn, cv2.CV_64F, DoG.Filter)

        #Calculating: Focus Metric Based on DoG
        [r,c] = imgIn.shape
        Focus_DoG[i] = np.sum(np.sqrt(imgOut**2))/float(r*c)


        #Calculating: First Derivative
        Out_dx1 = cv2.filter2D(imgIn, cv2.CV_64F, dx1)
        Out_dy1 = cv2.filter2D(imgIn, cv2.CV_64F, dy1)

        #Calculating: Focus Metric Based on First Derivative
        Focus_D1[i] = np.sum(np.sqrt(Out_dx1**2 + Out_dy1**2))/float(r*c)



#==============================================================================
#   Correcting the Shift (Optional but recommended for "p3" data)
#==============================================================================

#    if pupil_name == 'p3':
#        AD = AD - 0.25
#        AD = AD[2:]
#        Focus_DoG = Focus_DoG[2:]
#        Focus_D1 = Focus_D1[2:]

#==============================================================================
#   Normalizing the Focus Metrics to the Maximum Value to make comparision.
#==============================================================================

    Focus_DoG_norm = Focus_DoG/Focus_DoG.max()
    Focus_D1_norm = Focus_D1/Focus_D1.max()

#==============================================================================
#   Plotting    
#==============================================================================

    fig, ax = plt.subplots(2,1)  
    fig.set_size_inches(graph_size)  

    ax[0].plot(AD, Focus_D1_norm,'-or', label='1D')
    ax[0].vlines(x=0, ymin=ax[0].axes.get_ylim()[0], ymax=ax[0].axes.get_ylim()[1], linestyle='--', color='k')  
    ax[0].hlines(y=1, xmin=ax[0].axes.get_xlim()[0], xmax=ax[0].axes.get_xlim()[1], linestyle='--', color='k')
    ax[0].legend()

    ax[1].plot(AD, Focus_DoG_norm,'-og', label='DoG')
    ax[1].vlines(x=0, ymin=ax[1].axes.get_ylim()[0], ymax=ax[1].axes.get_ylim()[1], linestyle='--', color='k')  
    ax[1].hlines(y=1, xmin=ax[1].axes.get_xlim()[0], xmax=ax[1].axes.get_xlim()[1], linestyle='--', color='k')    
    ax[1].legend()

    plt.show()
    
asked by Azucena Calderón 24.02.2018 в 09:46
source

1 answer

0

The python error message tells you that it found an invalid character and shows you its hexadecimal code: 0xf3 .

That code corresponds to the vowel or accented ó using an encoding ISO-8859-1. Therefore, some strange things:

  • The first line of your code declares utf-8 . On the other hand, the character for which it complains corresponds to an ISO coding.
  • Weirder still, the code you have pasted is pure ASCII, it does not contain characters in other encodings (neither utf nor ISO). In that sense there would be more than the first line declaring the encoding.
  • Where has the letter ó come from that is causing problems? Most likely, it's inside one of the files you're reading. To better locate the fault you should indicate in which line the exception jumps. By the way, the error would be occurring when executing the program and not when compiling it. It is probably happening when you create the label for some of the graphics. It is difficult to help you to arrange it without further details.

        
    answered by 24.02.2018 в 12:29