Try my code:
import cv2
import numpy as np
from skimage.io import imread
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
img = imread('images/box.jpg')
img_gray = rgb2gray(img)
img_gray = np.float32(img_gray)
#cv2.imshow("Image",img)
#cv2.imshow("Gray Image",img_gray)
#Ix = cv2.Sobel(img_gray,cv2.CV_64F,1,0,ksize=5)
#Iy = cv2.Sobel(img_gray,cv2.CV_64F,0,1,ksize=5)
kernel_x = np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]])
kernel_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
Ix = cv2.filter2D(img_gray,-1,kernel_x)
Iy = cv2.filter2D(img_gray,-1,kernel_y)
Ixx = Ix**2
Ixy = Ix*Iy
Iyy = Iy**2
#cv2.imshow("Ixx",Ixx)
#cv2.imshow("Iyy Image",Iyy)
#cv2.imshow("Ixy Image",Ixy)
# Loop through image and find our corners
k = 0.05
height = img_gray.shape[0]
width = img_gray.shape[1]
harris_response = []
window_size = 6
offset = int(window_size/2)
for y in range(offset, height-offset):
for x in range(offset, width-offset):
Sxx = np.sum(Ixx[y-offset:y+1+offset, x-offset:x+1+offset])
Syy = np.sum(Iyy[y-offset:y+1+offset, x-offset:x+1+offset])
Sxy = np.sum(Ixy[y-offset:y+1+offset, x-offset:x+1+offset])
# Find determinant and trace, use to get corner response
det = (Sxx * Syy) - (Sxy ** 2)
trace = Sxx + Syy
r = det - k * (trace ** 2)
harris_response.append([x, y, r])
img_copy = np.copy(img)
thresh = 500
#sift = cv2.xfeatures2d.SIFT_create()
#kp,dc = sift.compute(img,None)
for response in harris_response:
x, y, r = response
if r > thresh:
img_copy[y, x] = [255, 0, 0]
plt.imshow(img_copy)
cv2.waitKey(0)
plt.show()