How can i get person class and segmentation from MSCOCO dataset? - dataset

I want to download only person class and binary segmentation from COCO dataset. How can I do it?

use pycocotools .
import library
from pycocotools.coco import COCO
load json file of coco annotation
coco = COCO('/home/office/cocoDataset/annotations/instances_train2017.json')
get category IDs of coco dataset
category_ids = coco.getCatIds(catNms=['person'])
get annotations of a single image
annotations = coco.getAnnIds(imgIds=image_id, catIds=category_ids, iscrowd=False)
here each person has different annotation, and i'th person's annotation is annotation[i] hence merge all the annotations and save it
if annotations:
mask = coco.annToMask(annotations[0])
for i in range(len(annotations)):
mask |= coco.annToMask(annotations[i])
mask = mask * 255
im = Image.fromarray(mask)
im.save('~/mask_name.png')

Related

JSON array keyerror in Python

I'm fairly new to Python programming and am attempting to extract data from a JSON array. Below code results in an error for
js[jstring][jkeys]['5. volume'])
Any help would be much appreciated.
import urllib.request, urllib.parse, urllib.error
import json
def DailyData(symb):
url = https://www.alphavantage.co/queryfunction=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo
stockdata = urllib.request.urlopen(url)
data = stockdata.read().decode()
try:
js = json.loads(data)
except:
js = None
jstring = 'Time Series (Daily)'
for entry in js:
i = js[jstring].keys()
for jkeys in i:
return (jkeys,
js[jstring][jkeys]['1. open'],
js[jstring][jkeys]['2. high'],
js[jstring][jkeys]['3. low'],
js[jstring][jkeys]['4. close'],
js[jstring][jkeys]['5. volume'])
print('volume',DailyData(symbol)[5])
Looks like the reason for the error is because the returned data from the URL is a bit more hierarchical than you may realize. To see that, print out js (I recommend using a jupyter notebook):
import urllib.request, urllib.parse, urllib.error
import ssl
import json
import sqlite3
url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo"
stockdata = urllib.request.urlopen(url)
data = stockdata.read().decode()
js = json.loads(data)
js
You can see that js (now a python dict) has a "Meta Data" key before the actual time series begins. You need to start operating on the dict at that key.
Having said that, to get the data into a table like structure (for plotting, time series analysis, etc), you can use pandas package to read the dict key directly into a dataframe. The pandas DataFrame constructor accepts a dict as input. In this case, the data was transposed, so the T at the end rotates it (try with and without the T and you will see it.
import pandas as pd
df=pd.DataFrame(js['Time Series (Daily)']).T
df
Added edit... You could get the data into a dataframe with a single line of code:
import requests
import pandas as pd
url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo"
data = pd.DataFrame(requests.get(url).json()['Time Series (Daily)']).T
DataFrame: The contructor from Pandas to make data into a table like structure
requests.get(): method from the requests library to fetch data..
.json(): directly converts from JSON to a dict
['Time Series (Daily)']: pulls out the key from the dict that is the time series
.T: transposes the rows and columns.
Good luck!
Following code worked for me
import urllib.request, urllib.parse, urllib.error
import json
def DailyData(symb):
# Your code was missing the ? after query
url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={}&apikey=demo".format(symb)
stockdata = urllib.request.urlopen(url)
data = stockdata.read().decode()
js = json.loads(data)
jstring = 'Time Series (Daily)'
for entry in js:
i = js[jstring].keys()
for jkeys in i:
return (jkeys,
js[jstring][jkeys]['1. open'],
js[jstring][jkeys]['2. high'],
js[jstring][jkeys]['3. low'],
js[jstring][jkeys]['4. close'],
js[jstring][jkeys]['5. volume'])
# query multiple times, just to print one item?
print('open',DailyData('MSFT')[1])
print('high',DailyData('MSFT')[2])
print('low',DailyData('MSFT')[3])
print('close',DailyData('MSFT')[4])
print('volume',DailyData('MSFT')[5])
Output:
open 99.8850
high 101.4300
low 99.6700
close 101.1600
volume 19234627
Without seeing the error, it's hard to know what exact problem you were having.

How does one call external datasets into scikit-learn?

For example consider this dataset:
(1)
https://archive.ics.uci.edu/ml/machine-learning-databases/annealing/anneal.data
Or
(2)
http://data.worldbank.org/topic
How does one call such external datasets into scikit-learn to do anything with it?
The only kind of dataset calling that I have seen in scikit-learn is through a command like:
from sklearn.datasets import load_digits
digits = load_digits()
You need to learn a little pandas, which is a data frame implementation in python. Then you can do
import pandas
my_data_frame = pandas.read_csv("/path/to/my/data")
To create model matrices from your data frame, I recommend the patsy library, which implements a model specification language, similar to R formulas
import patsy
model_frame = patsy.dmatrix("my_response ~ my_model_fomula", my_data_frame)
then the model frame can be passed in as an X into the various sklearn models.
Simply run the following command and replace the name 'EXTERNALDATASETNAME' with the name of your dataset
import sklearn.datasets
data = sklearn.datasets.fetch_EXTERNALDATASETNAME()

How to get random value from a file in python?

It would have been very gratifying to figure this out by myself but I haven't been able to.
I want to grab a random value from a text file that contains data in the form of a dictionary eg:
{'One': '1111111', 'Two': '2222222', 'Three': '3333333'}
I've tried a few variations, but code is currently:
from random import *
table = open('file.txt')
random_value = random.choice(table.values())
When I try and print 'random_value' (to see if it is working), I get the error:
AttributeError: 'file' object has no attribute 'values'
table is a file object, and thus you want to turn it into a dictionary. Here I use the ast module:
from random import choice # No need to import everything if you're going to use just one function
import ast
table = open('file.txt').read()
mydict = ast.literal_eval(table)
random_value = choice(mydict.values())

How to find the closest point in the DB using objectify-appengine

I'm using objectify-appengine in my app. In the DB I store latitude & longitude of places.
at some point I'd like to find the closest place (from the DB) to a specific point.
As far as i understood i can't perform regular SQL-like queries.
So my question is how can it be done in the best way?
You should take a look at GeoModel, which enables Geospatial Queries with Google App Engine.
Update:
Let's assume that you have in your Objectify annotated model class, a GeoPt property called coordinates.
You need to have in your project two libraries:
GeoLocation.java
Java GeoModel
In the code that you want to perform a geo query, you have the following:
import com.beoui.geocell.GeocellManager;
import com.beoui.geocell.model.BoundingBox;
import you.package.path.GeoLocation;
// other imports
// in your method
GeoLocation specificPointLocation = GeoLocation.fromDegrees(specificPoint.latitude, specificPoint.longitude);
GeoLocation[] bc = specificPointLocation.boundingCoordinates(radius);
// Transform this to a bounding box
BoundingBox bb = new BoundingBox((float) bc[0].getLatitudeInDegrees(),
(float) bc[1].getLongitudeInDegrees(),
(float) bc[1].getLatitudeInDegrees(),
(float) bc[0].getLongitudeInDegrees());
// Calculate the geocells list to be used in the queries (optimize
// list of cells that complete the given bounding box)
List<String> cells = GeocellManager.bestBboxSearchCells(bb, null);
// calculate geocells of your model class instance
List <String> modelCells = GeocellManager.generateGeoCell(myInstance.getCoordinate);
// matching
for (String c : cells) {
if (modelCells.contains(c)) {
// success, do sth with it
break;
}
}

get_by_id method on Model classes in Google App Engine Datastore

I'm unable to workout how you can get objects from the Google App Engine Datastore using get_by_id. Here is the model
from google.appengine.ext import db
class Address(db.Model):
description = db.StringProperty(multiline=True)
latitude = db.FloatProperty()
longitdue = db.FloatProperty()
date = db.DateTimeProperty(auto_now_add=True)
I can create them, put them, and retrieve them with gql.
address = Address()
address.description = self.request.get('name')
address.latitude = float(self.request.get('latitude'))
address.longitude = float(self.request.get('longitude'))
address.put()
A saved address has values for
>> address.key()
aglndWVzdGJvb2tyDQsSB0FkZHJlc3MYDQw
>> address.key().id()
14
I can find them using the key
from google.appengine.ext import db
address = db.get('aglndWVzdGJvb2tyDQsSB0FkZHJlc3MYDQw')
But can't find them by id
>> from google.appengine.ext import db
>> address = db.Model.get_by_id(14)
The address is None, when I try
>> Address.get_by_id(14)
AttributeError: type object 'Address' has no attribute 'get_by_id'
How can I find by id?
EDIT: It turns out I'm an idiot and was trying find an Address Model in a function called Address. Thanks for your answers, I've marked Brandon as the correct answer as he got in first and demonstrated it should all work.
I just tried it on shell.appspot.com and it seems to work fine:
Google Apphosting/1.0
Python 2.5.2 (r252:60911, Feb 25 2009, 11:04:42)
[GCC 4.1.0]
>>> class Address(db.Model):
description = db.StringProperty(multiline=True)
latitude = db.FloatProperty()
longitdue = db.FloatProperty()
date = db.DateTimeProperty(auto_now_add=True)
>>> addy = Address()
>>> addyput = addy.put()
>>> addyput.id()
136522L
>>> Address.get_by_id(136522)
<__main__.Address object at 0xa6b33ae3bf436250>
An app's key is a list of (kind, id_or_name) tuples - for root entities, always only one element long. Thus, an ID alone doesn't identify an entity - the type of entity is also required. When you call db.Model.get_by_id(x), you're asking for the entity with key (Model, x). What you want is to call Address.get_by_id(x), which fetches the entity with key (Address, x).
You should use long type in get_by_id("here").
Int type must have a error message.

Resources