0

I am trying to run a python script on wamp server. I have already configured Apache Web server to run Python CGI by making changes in Apache>https.conf file. Although I'm able to run simple python scripts containing print statements, but not the following code snippet:

realcode.py

    from __future__ import division
    from collections import Counter

    #!/Python27/python
    print "Content-type: text/html"
    print
    print "<html><head>"
    print ""
    print "</head><body>"
    print "hello from real code"

    import PIL
    from PIL import Image
    import matplotlib.pyplot as plt
    import numpy as np
    import sys, numpy
    import logging
    import cv2
    import os

    def drawMatches(img1, kp1, img2, kp2, matches):
        rows1 = img1.shape[0]
        cols1 = img1.shape[1]
        rows2 = img2.shape[0]
        cols2 = img2.shape[1]

        count=0
        arr_of_val_y=[]
        arr_of_val_x=[]
        q=0
        q1=0

        out = np.zeros((max([rows1,rows2]),cols1+cols2,3), dtype='uint8')
        out[:rows1,:cols1] = np.dstack([img1, img1, img1])
        out[:rows2,cols1:] = np.dstack([img2, img2, img2])

        for mat in matches:
            img1_idx = mat.queryIdx
            img2_idx = mat.trainIdx

            (x1,y1) = kp1[img1_idx].pt
            (x2,y2) = kp2[img2_idx].pt

            q=x2-x1
            q1=y2-y1

            arr_of_val_x.append(int(round(q)))
            arr_of_val_y.append(int(round(q1/10)*10))

            cv2.circle(out, (int(x1),int(y1)), 4, (255, 0, 0), 1)   
            cv2.circle(out, (int(x2)+cols1,int(y2)), 4, (255, 0, 0), 1)

            cv2.line(out, (int(x1),int(y1)), (int(x2)+cols1,int(y2)), (255, 0, 0), 1)

            count=count+1

        print "ARRAY IS"

        c=Counter(arr_of_val_y)

        m_percent=(max(c.values())/20)*100
        um_percent=100-m_percent
        
        slices=[m_percent,um_percent]
        acts=['MATCHED %d %%' % m_percent,'REM %d %%' % um_percent]
        cols=['r','k']
        explode=(0.05,0)
        plt.pie(slices,explode=explode,labels=acts,colors=cols,shadow=True,startangle=90)

        plt.title("RESULTS")
        plt.show()
        cv2.imshow('Matched Features', out)
        cv2.waitKey(0)
        cv2.destroyWindow('Matched Features')

        return out

    basewidth = 300
    img = Image.open('C:\\wamp\\bin\\apache\\apache2.4.9\\cgi-bin\\tiger.jpg')
    wpercent = (basewidth/float(img.size[0]))
    hsize = int((float(img.size[1])*float(wpercent)))
    img = img.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
    img.save('resized_file_1.jpg')

    basewidth = 300
    img = Image.open('C:\\wamp\\bin\\apache\\apache2.4.9\\cgi-bin\\tiger1.jpg')
    wpercent = (basewidth/float(img.size[0]))
    hsize = int((float(img.size[1])*float(wpercent)))
    img = img.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
    img.save('resized_file_2.jpg')

    img1 = cv2.imread('resized_file_1.jpg', 0) # Original image - ensure grayscale
    img2 = cv2.imread('resized_file_2.jpg', 0) # Rotated image - ensure grayscale

    orb = cv2.ORB(1000, 1.2)
    (kp1,des1) = orb.detectAndCompute(img1, None)
    (kp2,des2) = orb.detectAndCompute(img2, None)
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
    matches = bf.match(des1,des2)
    matches = sorted(matches, key=lambda val: val.distance)
    out = drawMatches(img1, kp1, img2, kp2, matches[:20])

    print "</body></html>"
Community
  • 1
  • 1
  • Most likely the problem is that at least one of the Python modules that you are trying to import is not available to the user that is running apache. I'm not sure how to fix this on Windows but you may find the Linux solution somewhat helpful http://stackoverflow.com/questions/39471295/how-to-install-python-package-for-global-use-by-all-users-incl-www-data/ – infinigrove Jan 23 '17 at 13:57
  • The link you mentioned is helpful in Linux. What can be the alternate for windows? – anushka bajpai Jan 24 '17 at 19:40
  • Sorry, I'm not sure how to go about fixing this in Windows. I'm guessing that it's the same basic problem as linux though. – infinigrove Jan 24 '17 at 22:59
  • I tried running a sample script using import statement. **sample.py** `#!/Python27/python print "Content-type: text/html" print print "" print "" print "" print "SAMPLE" import cv2 img=cv2.imread("C:\\wamp\\bin\\apache\\apache2.4.9\\cgi-bin\\money.jpg") img=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) cv2.imwrite("grey.png",img) print "" ` This code runs perfectly. But the above code doesn't. I don't understand why. – anushka bajpai Jan 24 '17 at 23:47
  • Are you getting any errors in your apache error log? If so what are they? – infinigrove Jan 25 '17 at 00:20
  • Yes. It shows error in File : C:\\Python27\\lib\\site-packages\\matplotlib\\__init__.py ImportError: No module named pwd\r – anushka bajpai Jan 25 '17 at 00:44
  • Kind of looks like what I suspected in that it's not finding all your modules. You can try checking out http://stackoverflow.com/questions/38949955/importerror-no-module-named-pwd-in-app-engine but I don't think that will help either. Sorry I'm not much help but I don't run apache or python much on windows. – infinigrove Jan 25 '17 at 00:59
  • Thanks for the help. :) – anushka bajpai Jan 25 '17 at 01:02

0 Answers0