0

In my script I save python 2D list(all are float values) in a json file. When I run the script in my machine saved json file contains the correct data. But when I run the python script in xampp server, json file only contains integer values.

I checked the code but no issue found. And the same code outputs two different json files.

When I run script in my machine;

 {"arr": [[5.7, 7.14, 5.72, 10.96, 15.82, 10.96, 15.78, 7.08], [5.76, 7.2, 15.72, 7.14, 15.76, 10.88, 5.8, 10.9], [4.98, 1.46, 4.98, 11.7, 14.38, 11.7, 14.44, 15.2, 29.14, 15.22, 29.12, 5.7, 21.42, 5.68, 21.34, 1.44], [17.52, 8.76, 23.18, 8.8, 23.16, 15.14, 17.46, 15.1], [21.4, 6.14, 28.74, 6.08, 28.78, 7.54, 21.44, 7.56], [5.02, 6.4, 15.82, 6.36, 15.88, 1.5, 21.28, 1.5, 21.36, 7.62, 29.06, 7.64, 29.06, 15.12, 23.28, 15.14, 23.24, 8.72, 17.48, 8.7, 17.4, 15.12, 14.52, 15.14, 14.46, 11.64, 5.06, 11.64], [5.02, 1.56, 15.72, 1.5, 15.76, 6.28, 5.06, 6.28]]}

When I run script in xampp server;

{"arr": [[5, 7, 5, 10, 15, 10, 15, 7], [5, 7, 15, 7, 15, 10, 5, 10], [4, 1, 4, 11, 14, 11, 14, 15, 29, 15, 29, 5, 21, 5, 21, 1], [17, 8, 23, 8, 23, 15, 17, 15], [21, 6, 28, 6, 28, 7, 21, 7], [5, 6, 15, 6, 15, 1, 21, 1, 21, 7, 29, 7, 29, 15, 23, 15, 23, 8, 17, 8, 17, 15, 14, 15, 14, 11, 5, 11], [5, 1, 15, 1, 15, 6, 5, 6]]}    

This is my code.

#!/usr/bin/env python
import sys;


import cv2
import codecs, json
dataset=[]
image = cv2.imread("C:/xampp/htdocs/"+sys.argv[1])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
edged = cv2.Canny(gray, 10, 250)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 20))
closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
cv2.waitKey(0)
(_, cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_CCOMP, 
cv2.CHAIN_APPROX_SIMPLE)
total = 0
lengths = 0;
i = 0
for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    converted_cnts_rooms = approx.squeeze()

    dataset.append([])
    for iterate in converted_cnts_rooms:
        x1, y1 = iterate.ravel()
        dataset[i].append(x1/50)
        dataset[i].append(y1/50)
    total += 1
    i += 1

cv2.waitKey(0)
converted_cnts = approx.squeeze()
for iterate in converted_cnts:
    x1,y1 = iterate.ravel()

data = {}
data.update({"arr":dataset})

with open('dataset.json', 'w') as outfile:
json.dump(data, outfile)

Appreciate your help...!!!

  • 1
    Could it be that you are running Python 2 on your xampp server? It seems to be the difference in division between Python 2 and 3 (see [this answer](https://stackoverflow.com/questions/2958684/python-division/2958717#2958717)). – ikkuh Sep 20 '18 at 11:56
  • ohhhh thanks a lot. If I change the python version to python 3.6, will it work? – john mathews Sep 20 '18 at 12:09
  • It worked by adding "from __future__ import division" for the very first line in the python file. Thanks again. You made my day.! – john mathews Sep 20 '18 at 12:20

0 Answers0