<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Automated Assessment of Car Damage Photos using SAS
Import the required packages including swat for accessing CAS actions
https
Step1:  Set up the environment and Connect to SAS from Python
Creating an Authinfo file
Step2:  Load images and resize
Step3:  Convert colours
Step4:  Apply noise reduction and binarization
Step5:  Compare images with the labeled images in the historical data
Using the similarity index for decision making
<ASSISTANT_TASK:>
Python Code:
# import the required packages 
from swat import *
from pprint import pprint
import numpy as np
import matplotlib.pyplot as plt
import cv2
# define the function to display the processed image files.
def imageShow(session, casTable, imageId, nimages):
    a = session.table.fetch(sastypes=False,sortby=[{'name':'_id_'}],table=casTable,to=nimages)
    fig = plt.figure(figsize=(20, 20))
    for i in range(nimages):
        imageData = a['Fetch'].ix[ i][ imageId]
        img_np = cv2.imdecode(np.fromstring( imageData, np.uint8),1)
        fig.add_subplot(1,nimages,i+1)
        plt.imshow(img_np)
        img_np[:,:,[0,2]]=img_np[:,:,[2,0]]
        plt.xticks([]), plt.yticks([])
Explanation: Automated Assessment of Car Damage Photos using SAS
Import the required packages including swat for accessing CAS actions
https://github.com/sassoftware/python-swat
End of explanation
# define the host machine and port for CAS connection: port is 5570 from Linux client and 8777 from Windows client.
hostmachine = 'my-viya-server.my-domain.com'
port = 8777
# authentication file on the client machine with user name and password (see the link above).
_authinfo = r"my-local-path\_authinfo"
# path on the Viya server where the image files to be processed are stored.
path_source_images    = "my-host-path-for-sources"
path_reference_images = "my-host-path-for-references"
# set up a CAS session.
conn = CAS(hostmachine, port, authinfo = _authinfo)
# load CAS image action set for processing images.
conn.loadactionset('image')
Explanation: Set up the environment and Connect to SAS from Python
Creating an Authinfo file:
http://go.documentation.sas.com/?docsetId=caspg&docsetTarget=n0i9fvsmff624fn1nh29sa6cx6lk.htm&docsetVersion=3.2&locale=en
End of explanation
conn.image.loadImages(casout={'name':'inputTable', 'replace':True}, path= path_source_images)
conn.image.processimages(casout={'name':'inputTable_resized', 'replace':True},
                       imagefunctions=[{'functionoptions':{'width':1000,'functiontype':'RESIZE','height':600}}],
                       imagetable={'name':'inputTable'})
imageTable = conn.CASTable('inputTable_resized')
imageShow(conn, imageTable, 0, 4)
Explanation: Load images and resize
End of explanation
r = conn.image.processImages(casout={'name':'resultingImages','replace':True},
        imagetable={'name':'inputTable_resized'},
        imagefunctions=[
            {'options':{'functiontype':'CONVERT_COLOR'}} #change color space
    ])
print(r)
outTable = conn.CASTable('resultingImages')
type(outTable)
imageShow(conn, outTable, 0, 4)
Explanation: Convert colours
End of explanation
r = conn.image.processImages(casout={'name':'resultingImages','replace':True},
        imagetable={'name':'inputTable_resized'},
        imagefunctions=[
            {'options':{'functiontype':'CONVERT_COLOR'}}, #change color space
        
            {'options':{'functiontype':'BILATERAL_FILTER', #noise reduction
                        'diameter':13,'sigmacolor':30,'sigmaspace':30}},
        
            {'options':{'functiontype':'THRESHOLD', #image binarization
                        'type':'OTSU','value':125}},
            {'options':{'functiontype':'LAPLACIAN', #edge detection with the Laplace operator
                        'kernelsize':12}}
           
    ])
print(r)
outTable = conn.CASTable('resultingImages')
imageShow(conn, outTable, 0, 4)
outTable.head(4)
Explanation: Apply noise reduction and binarization
End of explanation
# Process reference files to compare.
conn.image.loadImages(casout={'name':'inTable', 'replace':True}, path= 'path_reference_images')
conn.image.processImages(casout={'name':'refTable','replace':True},
        imagetable={'name':'inTable'},
        imagefunctions=[{'functionoptions':{'width':1000,'functiontype':'RESIZE','height':600}}, # resize
            {'options':{'functiontype':'CONVERT_COLOR'}}, #change color space
        
            {'options':{'functiontype':'BILATERAL_FILTER', #noise reduction
                        'diameter':13,'sigmacolor':30,'sigmaspace':30}},
            {'options':{'functiontype':'THRESHOLD', #image binarization
                        'type':'OTSU','value':125}}
           
    ])
# Compare reference and source images to find the similarity index.
results = conn.image.compareImages(
        casOut={
            "name":"output",
            "replace":True
        },
        pairAll=True,
        referenceImages={
            "table":{
            "name":'refTable'
        }},
        sourceImages={
            "table":{
            "name":'resultingImages'
        }}
    )
scoreTable = conn.CASTable("output")
del scoreTable['_channel4_'] 
del scoreTable['_channel3_']
print(results)
print(scoreTable.head())
# end the CAS session.
conn.session.endsession()
Explanation: Compare images with the labeled images in the historical data
Using the similarity index for decision making
End of explanation
<END_TASK>