function [match,match_suit] = OCR(str)
    %match is the ranking match
    match = 'none';
    %match_suit is the suit match
    match_suit = 'none';
    %ratio_suit is used to determine the suit based off of pixels
    ratio_suit = 0;

    %read the image
    try
        a = imread(str);
    catch err
        %fprintf('No file at %s\n', str);
        return;
    end
    %apply thresholding and make binary image
    BW_A = im2bw(a, 0.35);
    %gather image info for trimming (for resolution formatting)
    info = imfinfo(str);
    %adjust image by trimming off top and bottom edges for 176x144 ratio
    if(info.Height/176 > info.Width/144)
        scale = info.Width/144;
        trim = info.Height - (scale*176) +1;
        BW_A_T = BW_A(round(trim/2):end,:);
        BW_A_T = BW_A_T(1:round(end-(trim-trim/2)),:);
    end
    %adjust resolution while maintaining ratio
    card = imresize(BW_A_T, [176 144]);
    %tempstore = card;
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %Crop out the character itself
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    %crop out white space to the left of character
    cut_left = 1;
    for x=1:144,
        pixel = 1;
        for y=1:176,
            pixel = pixel & card(y,x);
        end
        if(pixel == 0 && cut_left==1)
            cut_left = x-1;
        elseif(pixel ==1)
            %fprintf('ONLY WHITE at column %i\n', x);
        end
    end
    card = card(:,cut_left:end);
    
    %crop out white space to the right of character
    cut_right = 1;
    for x=1:(144-cut_left),
        isWhite = 1;
        for y=1:176,
            pixel = card(y,x);
            %fprintf('card(%i,%i)=%i\n', y,x,pixel);
            if(pixel == 0)
                %fprintf('BLACK at column %i\n', x);
                isWhite = 0;
            elseif(isWhite==1 && y==176 && cut_right == 1)
                %fprintf('FIRST WHITE at column %i\n', x);
                cut_right = x;
            end
        end
    end
    card = card(:,1:cut_right);
    
    %crop out white space to the top of character
    cut_top = 1;
    isBlock = 1;
    isAce = 0;
    reachBlack = 0;
    last_pix = 1;
    for y=1:176,
        pixel = 1;
        continuous = 0;
        for x=1:cut_right,
            pixel = pixel & card(y,x);
            %check to make sure not black corner on Ace
            if(last_pix ~= card(y,x))
                continuous = continuous + 1;
            end
            if(continuous > 2)
                isBlock = 0;
            end
            last_pix = card(y,x);
        end
        %if reach first black pixel
        if(pixel == 0 && cut_top==1)
            cut_top = y-1;
            reachBlack = 1;
        %if reach first white pixel after block
        elseif(pixel ==1 && reachBlack ==1 && isBlock == 1)
            isAce = 1;
            cut_top = y;
        end
    end
    card = card(cut_top:end,:);
    
    %crop out white space to the bottom of character
    cut_bot = 1;
    start_char =0;
    for y=1:176-cut_top,
        isWhite = 1;
        for x=1:cut_right,
            pixel = card(y,x);
            %fprintf('card(%i,%i)=%i\n', y,x,pixel);
            if(pixel == 0)
                %fprintf('BLACK at column %i\n', x);
                isWhite = 0;
                start_char = 1;
            elseif(isWhite==1 && x==cut_right && cut_bot == 1 && start_char == 1)
                %fprintf('FIRST WHITE at column %i\n', x);
                cut_bot = y;
            end
        end
    end
    suit = card(cut_bot:end,:);
    rank = card(1:cut_bot,:);
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %THE FOLLOWING IS CALCULATIONS FOR THE RANK OF THE CARD
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %count black versus white pixels
    B = 0;
    W = 0;
    for x=1:cut_right,
        for y=1:cut_bot,
            if(rank(y,x) == 0)
                B = B + 1;
            else
                W = W + 1;
            end
        end
    end
    ratio = B/(B+W);
    if(ratio==0)
        match = 'A';
        match_suit = 'spades';
        return;
    end
    
    %count number of color inversions top down (center aligned)
    prev_pix = 1;
    vert_flip =0;
    for y=1:cut_bot,
        curr_pix = rank(y,int8(cut_right/2));
        %fprintf('curr %i prev %i\n', curr_pix, prev_pix);
        if(curr_pix ~= prev_pix)
            vert_flip = vert_flip + 1;
        end
        prev_pix = curr_pix;
    end

    %count number of color inversions left to right (top 1/4)
    prev_pix = 1;
    quart1_flip =0;
    three_or_five=0;
    for x=1:cut_right,
        curr_pix = rank(int8(cut_bot/4),x);
        if(curr_pix ~= prev_pix)
            %special case for distinguishing between a 3 or 5
            if(quart1_flip == 0 && x<cut_right/2)
                three_or_five = 5;
            elseif(quart1_flip == 0 && x>cut_right/2)
                three_or_five = 3;
            end
            quart1_flip = quart1_flip + 1;
        end
        prev_pix = curr_pix;
    end

    %count number of color inversions left to right (center aligned)
    prev_pix = 1;
    hor_flip =0;
    for x=1:cut_right,
        curr_pix = rank(int8(cut_bot/2),x);
        if(curr_pix ~= prev_pix)
            hor_flip = hor_flip + 1;
        end
        prev_pix = curr_pix;
    end

    %count number of color inversions left to right (bottom 3/4)
    prev_pix = 1;
    quart2_flip =0;
    for x=1:cut_right,
        curr_pix = rank(int8(3*cut_bot/4),x);
        if(curr_pix ~= prev_pix)
            quart2_flip = quart2_flip + 1;
        end
        prev_pix = curr_pix;
    end

    %a check flag for 9 or 6
    nine_six=0;
    %assign match based upon flips
    if(isAce == 1)
        match='A';
    elseif(vert_flip==6 && quart1_flip==4 && hor_flip==2 && quart2_flip==2)
        match='2';
    elseif((vert_flip==6 || vert_flip==8) && quart1_flip==2 && quart2_flip==2 && three_or_five == 3)
        match='3';
    elseif(vert_flip==4 && quart1_flip==4 && hor_flip==4 && quart2_flip==2)
        match='4';
    elseif(vert_flip==6 && quart1_flip==2 && quart2_flip==2 && three_or_five == 5)
        match='5';
    elseif(vert_flip==6 && quart1_flip==4 && hor_flip==4 && quart2_flip==4)
        nine_six=1;
    elseif(vert_flip==4 && hor_flip==2 && quart2_flip==2)
        match='7';
    elseif(vert_flip==6 && quart1_flip==4 && hor_flip==2 && quart2_flip==4)
        match='8';
    elseif(vert_flip==6 && quart1_flip==4 && hor_flip==4 && quart2_flip==4)
        nine_six=1;
    elseif(vert_flip==4 && quart1_flip==6 && hor_flip==6 && quart2_flip==6)
        match='10';
    elseif(vert_flip==2 && quart1_flip==2 && hor_flip==2 && quart2_flip==4)
        match='J';
    elseif(vert_flip==6 && quart1_flip==4 && hor_flip==4 && quart2_flip==6)
        match='Q';
    elseif(vert_flip==2 && quart1_flip==4 && hor_flip==2 && quart2_flip==4)
        match='K';
    end

    %special case handler for 9 or 6 (and sometimes Queen)
    %count number of color inversions left to right (top 1/3)
    B_top =0;
    W_top =0;
    B_bot =0;
    W_bot =0;
    if(nine_six==1)
%         for x=1:cut_right,
%             for y=1:int8(cut_bot/4),
%                 if(card(y,x) == 0)
%                     B_top = B_top + 1;
%                 else
%                     W_top = W_top + 1;
%                 end
%             end
%             for y=int8(3*cut_bot/4):cut_bot,
%                 if(card(y,x) == 0)
%                     B_bot = B_bot + 1;
%                 else
%                     W_bot = W_bot + 1;
%                 end
%             end
%         end
%         ratio_top = B_top/(B_top+W_top);
%         ratio_bot = B_bot/(B_bot+W_bot);
%         if(ratio_top<ratio_bot)
%             match='6';
%         else
%             match='9';
%         end
        two_line=0;
        for y=1:cut_bot,
            prev_pix = 1;
            flip =0;
            for x=1:cut_right,
                curr_pix = rank(y,x);
                if(curr_pix ~= prev_pix)
                    flip = flip + 1;
                end
            prev_pix = curr_pix;
            end
            if(flip == 4)
                two_line = 1;
            end
            if(two_line == 1 && flip == 2)
                if(y<cut_bot/2)
                    match='6';
                elseif(y>cut_bot/2)
                    match='9';
                end
            elseif(flip == 6)
                match='Q';
            end
            if(strcmp(match,'none')==0)
                break;
            end
        end
    end
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %THE FOLLOWING IS CALCULATIONS FOR THE SUIT OF THE CARD
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if(ratio~=0)
        %crop out white space to the left of SUIT
        cut_left_suit = 1;
        for x=1:cut_right,
            pixel = 1;
            for y=1:176-(cut_top+cut_bot),
                pixel = pixel & suit(y,x);
            end
            if(pixel == 0 && cut_left_suit==1)
                cut_left_suit = x-1;
            elseif(pixel ==1)
                %fprintf('ONLY WHITE at column %i\n', x);
            end
        end
        suit = suit(:,cut_left_suit:end);
        
        %crop out white space to the right of SUIT
        cut_right_suit = 1;
        for x=1:cut_right-cut_left_suit,
            isWhite = 1;
            for y=1:176-(cut_top+cut_bot),
                pixel = suit(y,x);
                %fprintf('card(%i,%i)=%i\n', y,x,pixel);
                if(pixel == 0)
                    %fprintf('BLACK at column %i\n', x);
                    isWhite = 0;
                elseif(isWhite==1 && y==176-(cut_top+cut_bot) && cut_right_suit == 1)
                    %fprintf('FIRST WHITE at column %i\n', x);
                    cut_right_suit = x;
                end
            end
        end
        %account for close crops
        if(cut_right_suit == 1)
            cut_right_suit = cut_right-cut_left_suit;
        end
        suit = suit(:,1:cut_right_suit);
        
        %crop out white space to the top of character
        cut_top_suit = 1;
        for y=1:176-(cut_top+cut_bot),
            pixel = 1;
            for x=1:cut_right_suit,
                pixel = pixel & suit(y,x);
            end
            if(pixel == 0 && cut_top_suit==1)
                cut_top_suit = y-1;
            elseif(pixel ==1)
                %fprintf('ONLY WHITE at column %i\n', x);
            end
        end
        suit = suit(cut_top_suit:end,:);
        
        %crop out white space to the bottom of character
        cut_bot_suit = 1;
        for y=1:176-(cut_top+cut_bot+cut_top_suit),
            isWhite = 1;
            for x=1:cut_right_suit,
                pixel = suit(y,x);
                %fprintf('card(%i,%i)=%i\n', y,x,pixel);
                if(pixel == 0)
                    %fprintf('BLACK at column %i\n', x);
                    isWhite = 0;
                elseif(isWhite==1 && x==cut_right_suit && cut_bot_suit == 1)
                    %fprintf('FIRST WHITE at column %i\n', x);
                    cut_bot_suit = y;
                end
            end
        end
        %account for close crops
        if(cut_bot_suit == 1)
            cut_bot_suit = 176-(cut_top+cut_bot+cut_top_suit);
        end
        suit = suit(1:cut_bot_suit,:);

        %count black versus white pixels
        B_suit = 0;
        W_suit = 0;
        for x=1:cut_right_suit,
            for y=1:cut_bot_suit,
                if(suit(y,x) == 0)
                    B_suit = B_suit + 1;
                else
                    W_suit = W_suit + 1;
                end
            end
        end
        ratio_suit = B_suit/(B_suit+W_suit);
        
        if(ratio_suit > 0.53)
            %match_suit = '?';
            match_suit = 'hearts';
        elseif(ratio_suit < 0.44)
            %match_suit = '?';
            match_suit = 'diamonds';
        elseif(ratio_suit > 0.44 && ratio_suit < 0.495)
            %match_suit = '?';
            match_suit = 'clubs';
        else
            %match_suit = '?';
            match_suit = 'spades';
        end
        
    end
    

    imshow(card)
end