Back in 2002, when he was head of Civil Police in Rio de Janeiro, Zaqueu Teixeira caused a minor social earthquake. A group of streetwalkers had been complaining of being roughed up in the streets, and Teixeira decided to investigate. "I brought them into the station to hear their story," he says. "The entire police command was in shock." Not only were the victims prostitutes, they were also transvestites—second-class citizens to many Brazilians. Not least to Rio's cops.

What a difference a decade makes. This week, a group of 50 of Rio's finest filed into an auditorium in the art deco building that houses the state secretariat of public security for a morning of lectures, debate, and culture shock. This was the latest round of a special training seminar designed to instruct career officers on how to serve and protect a burgeoning Brazilian demographic: the gay, lesbian, bisexual, and transgender community.

Rio's top brass was there, as were public safety secretary José Mariano Beltrame and a handful of other Rio grandees. "Everybody behaved. No one offended anyone else or made ugly faces," says Jane di Castro, a transvestite singer, who performed the national anthem, danced in the aisles and joked with police at the gathering. "Imagine police taking up our cause! I was very pleasantly surprised," confesses the 67-year-old stage artist, who was born Luiz de Castro.

She wasn't the only one. Though Brazil has earned a deserved reputation for its easygoing social relations, where class and cultural barriers break down at the first strophes of samba, the reality on the ground is sometimes starkly different. Last year, 338 gay men and lesbians were murdered nationwide, a steep jump from the year before. A government anti-homophobia hotline, Dial 100, logged 3,017 emergency calls in 2012.

Instead of keeping the peace, police too often were part of the problem. "I used to be afraid of you guys," di Castro joked with the cops at the Rio seminar. "This gathering is a breakthrough."

Some six years in the making, Rio's sensitivity training has become a pillar of community policing, and a policy centerpiece for the ambitious governor, Sergio Cabral. Some 5,000 military police—as Brazil's uniformed beat cops are known—have taken the seminar, sitting in on a series of lectures on "gender diversity" by sociologists, anthropologists, social workers, and representatives of gay, lesbian, and transgender groups.

The new round of seminars will train another 6,200 police, both beat cops and plainclothes detectives. "This is an important initiative to help break down social bias and discrimination," says Teixeira, who left the police force to head the state office of social assistance and human rights.

In many ways, Rio was ripe for reeducation. With its beaches and carnival parades, where merrymakers of all colors and classes mingle, this storied seaside city of 10 million people has often been a bellwether for this emerging Latin American democracy. When the long (1964–85) dictatorship in Brazil gave way to free and fair elections, civil society was reborn. Labor unions, green groups, indigenous, and Afro-Brazilian communities have spoken up and demanded their place in the "new Brazil." Now, gays and lesbians are staking claims of their own.

In 2011 the Supreme Court by unanimous vote sanctioned homosexual unions (though not marriage) and granted same-sex couples identical rights to heterosexuals, including the right to adopt children and inherit assets of a deceased spouse. This is part of a wider move in Latin America that has swept aside legal barriers built on centuries of hidebound custom and law in a region dominated by the Roman Catholic Church and, more recently, reinforced by fundamentalist evangelical Protestant groups.

Though the Brazilian Congress is still weighing a bill that would make homophobia a crime, the state of Rio has blazed the way by keeping police records of hate crimes against homosexuals. Though officials say the database is too recent to draw conclusions, the pushback against homophobia has already reaped some positive results, encouraging victims of violence to come forward.

Some 12,000 people called on Rio's four citizen centers for the lesbian, gay, and transgender community between 2009 to March 2012, while state officials recorded 1,000 acts of aggression in the same period. "We need more numbers to know whether violence is rising or falling, but it's clear that the gay community is speaking up while reluctance to reporting hate crimes is falling," says Claudio Nascimento, who supervises individual and collective rights for Rio's human rights secretariat.

Reeducating Brazil will take time. Complicating the task is a surge in ultraconservative Protestant evangelical churches, where fundamentalist pastors regularly rail against gays and lesbians. Many fundamentalistas have taken to politics, such as federal lawmaker Marco Feliciano, a pastor who also heads the congressional human rights commission and who has trumpeted a project to "cure homosexuals."

Breaking down barriers within the police has already begun. Di Castro, for her part, is optimistic. "During the dictatorship, when gays and transvestites were arrested, they often had to put out or be beaten," says Di Castro. "Now the police are busy repressing drug traffickers."

"The cops may not be my best friends, but they aren't my enemies," she adds. "When I go down to the police station, I am treated very well. As long as they don't call me Luiz."